Can there exist two main methods in a Java program? Only by the difference in their arguments like:
public static void main(String[] args)
and second can be
public static void main(StringSecond[] args)
If it is possible,which Method will be used as the entry point? How to identify this? please help.
the possibility of two main(String[] args) methods within the same scope create confusion for the JVM. It fails to use them as overloaded methods. So the signatures in terms of parameters) must be different.
The below code in file "Locomotive.java" will compile and run successfully, with the execution results showing
As mentioned in above post, the overload rules still work for the main method. However, the entry point is the famous psvm (public static void main(String[] args))
Only
public static void main(String[] args)
counts. This is the only signature considered to be the true main() (as the program entry point, I mean).In Java, you can have just one
public static void main(String[] args)
per class. Which mean, if your program has multiple classes, each class can havepublic static void main(String[] args)
. See JLS for details.