So I just tried excluding String[]
args from the main
method
It compiled alright !
But JVM is showing an exception
Why did it compile when String[]
args HAS to be included every time ?
What is going on here ? Why won't it show a compilation error ?
typing this made me think that may be compiler did not see it as THE main
method ..is that so ?
If that is the case..why not ? I mean isn't there supposed to be just one main method that MUST have String[]
args as the argument ?
Not every class has to have a
public static void main(String[] args)
method, only the one you want to run from the JVM. The result is that your class will compile with no error if it findspublic static void main()
but will throw an exception if you try and run it with the JVM because it can not find the entry point of the program.Bottom line the entry point of your program must be
public static void main(String[] args)
which must be located in at least one of your .java files.Note you can have multiple
public static void main(String[] args)
methods in your code (one per class) the advantage is you can use these to test and debug your classes individually.The JVM is looking for a very special main method to run. Only the signature
is found. All other methods with name
main
are just "normal" methods.Here's a quote from Java Tutorials/Getting Started:
So to be precise, the
main
method is defined by Java as an application entry point. Not everything is an application, and not everymain
method is an application entry point. Applets, for example, don't need amain
, because it's started with a different mechanism.Note also that since the introduction of varargs, you can actually declare your application entry point as follows:
This works because underneath the syntactic sugar, varargs in Java is implemented as arrays.
See also
Related questions
main
methodstatic
?main
method?main()
in java isvoid
?main()
?main()
,init()
, orrun()
?Java supports method overloading. This means you can have several methods with the same name, but different arguments.
Having said that, when you run
java ClassName
, Java looks inClassName.class
for a method with the signaturepublic static void main (String[])
(it doesn't care what theString[]
variable is named) and runs it. If it doesn't find one, Java will bomb out with the following exception:Isn't that overloading? It's fully legal to define a method
It's just not the entry point the JVM will be looking for.
Overloading is the ability to have multiple methods with the same name but different arguments. The compiler in fact creates a name based on the method name and the arguments.
So a main(String[]) would be called, to the compiler, something like main_String_arr, and main() would be called something like main.
You are correct. The runtime is looking for a main method that takes a string array as a parameter, and isn't finding it.
The fact that you have a main method that doesn't take a string array is irrelevant. just like any other method, you can create multiple versions of
main()
that take different parameters - the runtime will just ignore them.