Reputation: 294
I got the below code from internet and even though there are 3 main methods, if I run the app from command prompt:
java MainTest 1 2 3
I would get the output:
String main 1
public class MainTest {
public static void main(int [] args) {
System.out.println("int main " + args[0]);
}
public static void main(Object[] args) {
System.out.println("Object main " + args[0]);
}
public static void main(String[] args) {
System.out.println("String main " + args[0]);
}
}
Is there any scenario where more than 1 main method is needed in the same class?
I have not used yet the main method with int
, Object [] args
? Is that even possible? I thought it was only possible with String[] args
.
I was expecting getting an error message at runtime. Why I did not get any error when the application ran? Does it mean that the parameters passed are always parsed to String
and Java gets the main method that suits best?
Thanks a lot!!
Upvotes: 0
Views: 3409
Reputation: 298599
The name main
is not reserved by the Java Programming Language. It’s an ordinary identifier and thus, the same rules as with all other identifiers apply. You can have multiple methods named main
as long as they have different parameter types. That’s called method overloading.
The special meaning of a certain main
method is defined by the Launcher which initializes your application and this startup procedure defines the rules. Note that there might be different, implementation specific, ways to launch an application, e.g. Applet
s don’t have a main
method and JavaFX provides its own Launcher that works without a main
method. The same applies to several server-side Java technologies. This doesn’t prevent you from declaring main
methods when using such technologies, but they don’t have a special meaning.
So the well-known java
launcher, using command line arguments, searches for a special method named main
having the raw signature public static void main(java.lang.String[])
in the specified main class. Note that there were earlier Java versions whose launcher didn’t care about the presence or absence of the public
modifier. All that matters to the launcher is, whether it finds such a main
method. It doesn’t care about other methods of the same name and different signature. It also doesn’t care whether other classes have an appropriate main
method. All it does, is searching for an appropriate method in the specified class.
Since only the raw (bytecode level) signature of the method matters, it is irrelevant, which source code form it has, as long as it got compiled to a method with the appropriate signature. Thus, the following declarations will lead to a main
method, appropriate for launching a Java application:
public static void main(String[] arg) {}
public static void main(String arg[]) {} // source code variantion don’t matter
public static void main(String... arg) {} // varargs are compiled to an array type
public static final void main(String[] arg) {} // final is irrelevant
public interface Example {
static void main(String[] arg) {} // public is implied (Java 8)
}
public static <T extends String> void main(T[] arg) {} // raw parameter type is String[]
Note that with the last variant, some IDEs failed to launch the application (despite it works from command line), showing that the actual behavior dependents on the implementation of the launcher.
In contrast, the following doesn’t work:
class String{}
public static void main(String[] arg) // the parameter type is not java.lang.String[]
demonstrating, that not the source code form but the compiled raw signature matters. When trying to run the last example, the launcher will complain that it doesn’t find the required method public static void main(String[])
, not telling you that there is a different, non-matching main
method. As said, it doesn’t care about other methods of that name.
Upvotes: 1
Reputation: 26077
JVM
will only recognize below as a main method
.
public static void main(String[] args){} will be used when your class is launched by the JVM.
Also public static void main(String args[]) {}
can be used by JVM.
public static void main(String[] args) {
System.out.println("String main " + args[0]);
}
rest are overloaded
methods
Also we can use a varargs
signature, as that's equivalent from a JVM
standard
public static void main(String... args)
The positions of public and static
may change.
From JavaDocs
Upvotes: 1
Reputation: 70999
You can write as many static main methods as you like, but the JVM is looking for a specific main method signature.
public static void main(String[] args)
Any other main method will exist (and can be called by some other Java element) but those other main methods will not be called by the JVM at startup time.
Upvotes: 0
Reputation: 658
you are defining simple methods, JVM will not call them directly except public static void main(String[] args) . so it's upto your requirement in which you need a method with overloading concept and it's static so it can be called directly using the class name.
as per my understanding, it's up to your requirement only.
Upvotes: 0
Reputation: 4592
In java the method which is having exactly below signature :
public static void main(String[] args)
Having special rights and considered the Main method. Other are just overloaded methods.
Upvotes: 0
Reputation: 13854
The example shows overloading
main method is overloaded.
JVM only looks for main method which is static and it should accept only one argument of String array. So you are getting String main
as output
Is there any scenario where more than 1 main method is needed in the same class?
It only depends on your requirement
I have not used yet the main method with int, Object [] args? is that even possible? I thought it was only possible with String[] args
I think you want to call the main method with Object[] argument. public static void main(Object[] args) { is like any other simple method and you can call it by MainTest.main(new Object{1,2})
I was expecting getting an error message at runtime. Why I did not get any error when the application ran? Does it mean that the parameters passed are always parsed to String and Java gets the main method
The code is perfectly fine and so you are not getting any error.The code describes an example of overloading of main method
Upvotes: 2