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 ?
Correct. There is no compile error because you're perfectly free to have all kinds of methods named
main
. But when you start the JVM and give it a "main class", then it will look for a methodstatic public void main(String[])
in that class, and if it does not find such a method, it aborts with an exception.This allows you to have multiple main methods in your program and is really the only thing that makes sense if you think about it: Applications can be composed from classes and JAR files from lots of different sources, written by different people at different times, so in many cases you can't really have a single, designated "main class" right from the start.
It's still a valid method. For example, you could have a static method called "main" with an int parameter if you wanted:
The compiler doesn't complain because it's a valid method! It's just that, in order to run a Java program, Java looks for a static method called "main" with a single String array argument.
Yes..
The Java compiler will look for the same method signature to consider it a
main
Writing any function that has the same name but another parameters will result in overloading functions..
Overloaded functions are not the same..!!
The case in C# is somehow different..
At last, you must make sure that your main is like that:
You can have many methods named main, but only one can be THE main one - entry point to the program. It is the one with String[] args.
Important points:
Following is the signature of the main method:
main method can be overloaded.
Multiple classes can contain the main method inside a single compilation unit (and so all of them will be called executable classes)
The class containing the main method may or may not be public.
By mistake, if you were to omit the static keyword (or the signature differs in any way), compilation would be done but a runtime error will occur.
From my blog:
Java: Important points about the main method
To try to answer "why is it legal to compile without a proper main method" it's because not every java project is a stand alone application that can be run. Some are just libraries, where other programs will include them as jar files and use their code, but they don't "run" themselves. Others may be web applications, where they are deployed onto a web server that has already been started, only the server itself actually has a proper "main" method. The web application project is opened up and executed by it.
The compiler didn't really know at compile time that you were intending to try to run your code as a stand along application.