In Java why we write public static void main(String args[])
why not main()?
Answers were Sorted based on User's Feedback
Answer / guest
main() will be treated as just another method.
but public static void main(String args[])
is treated as entry point of an java application
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / shiva
In entire java program main() executes only once, so that
so that it is static.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / praveen
PUBLIC: in this we should access the function inside d class
or outside d class
STATIC:if any method is invoking obj is must.but using
static no obj is necessary.
VOID: void means nothig.in main method we should not declare
any value. so void is return type.
STRING[]ARGS:in java every input consider as a string
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / kedar
static methods can be accessed without creating object object of class, we know that file name of java program is "classname.java", "classname" is the class which contain main function, so it is easier for compiler to locate class which contain main(), and because of static nature it can execute it.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / anil kumar.kommmisetty
In java we using a main method
i.e public static void main(String args[])
public > This is used for any where we can access.
static > This is used for constat and then first compile to
start.
void > should not return any value.
main > where to start the compile the program to tell.
String > it is a class.
args[] > this is used to tell the when ever u enter the
data that is treated as arguments to that program.
Is This Answer Correct ? | 4 Yes | 4 No |
Answer / gowrishankar
The Main method is the method in which execution to any java
program begins.
A main method declaration looks as follows:
public static void main(String args[]){
}
The method is public because it be accessible to the JVM to
begin execution of the program.
It is Static because it be available for execution without
an object instance. you may know that you need an object
instance to invoke any method. So you cannot begin execution
of a class without its object if the main method was not
static.
It returns only a void because, once the main method
execution is over, the program terminates. So there can be
no data that can be returned by the Main method
The last parameter is String args[]. This is used to signify
that the user may opt to enter parameters to the java
program at command line. We can use both String[] args or
String args[]. The Java compiler would accept both forms.
Command line argument
Any number of arguments can be passed to the java program
through command line. While running the program any things
written after the name of the class are the command line
arguments. Arguments are delimited by the space.
This sample code prints all the command line arguments to
the console.
public class CmdLn{
public static void main(String[] args) {
System.out.println("d");
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
Java CmdLn arg1 arg2 arg3 arg4
Output:
arg1
arg2
arg3
arg4
so,if we simply give the main() function program wont work
without the access specifier.
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / rahul
The public keyword is an access specifier, which allows the
programmer to control the visibility of class members. When
a class member is preceded by public, then that member may
be accessed by code outside the class in which it is
declared.In this case, main( ) must be declared as public,
since it must be called by code outside of its class when
the program is started.
The keyword static allows main( ) to be called without
having to instantiate a particular instance of the class.
This is necessary since main( ) is called by the Java
interpreter before any objects are made.
The keyword void simply tells the compiler that main( )
does not return a value. As you will see, methods may also
return values.
about strings[] It takes no of arguements & stored in an
array ,input is of integer type it is taken as string. so
main takes arguments of type string.
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / xyz
Public -> Because we want to access the main from the out side of the class generally when we run the java program at that time it call the main method.
Static ->Because it will create only one copy of main method and we can call with class name so when we run program it will
run main method using passed program name like name.main();
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / amardip sabale
we write public static void main() because
public:for access of main function all over the program
static:main is also one type of method,and for calling this
method while running our program without creation of any
object
void:because it is not return anything
String args[]:because when we run our program then always
we pass at least two command line arguments(java and
program name)for collecting them we have to write it
*THANKS*
Is This Answer Correct ? | 0 Yes | 0 No |
why should we get the following error ? Exception in main method NoClassDefFoundError:classname could anyone give the detail clarification on how java compiler can look for .class file?
What is difference between equals and hashcode method?
Compare Mutex and Semaphore in java.
What do you mean by thread safe?
what is features of jdk 1.5?
2 Answers Accenture, Satyam, TCS,
Why main() method is public, static and void in java ?
Is overriding possible in java?
why abstract class will have a constructor?
How objects are stored in java?
When should a function throw an exception?
0 Answers Thomson Reuters, Virtusa,
What does method mean?
what is difference between validation.xml & validation rules.xml?