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.
As long as method parameters (number (or) type) are different, yes they can. It is called overloading.
only main method with single
String[]
(or)String...
as param will be considered as entry point for the program.Yes! Any class in Java can have multiple main methods. It's called Overloading (Overloaded methods are methods with same name but with different signatures) but there should only be one main method with parameters like this :- (String[] args) or (String args[])
For example :-public class E {
}
The output of the above program will be "Print B" as only that main method contains the correct method signature identified by the Javac compiler and it compiles that only and leaves the rest.
That would be compilable code, as long as
StringSecond
was a class. However, if by "main method" you mean a second entry point into the program, then the answer to your question is still no. Only the first option (public static void main(String[] args)
) can be the entry point into your program.Note, however, that if you were to place a second
main(String[])
method in a different class (but in the same project) you could have multiple possible entry points into the project which you could then choose from. But this cannot conflict with the principles of overriding or overloading.Also note that one source of confusion in this area, especially for introductory programmers, is that
public static void main(String[] args)
andpublic static void main(String ... args)
are both used as entry points and are treated as having the same method signature.Yes it is possible to have two main() in the same program. For instance, if I have a class Demo1 as below. Compiling this file will generate Demo1.class file. And once you run this it will run the main() having array of String arguments by default. It won't even sniff at the main() with int argument.
But if I add another class named Anonym and save the file as Anonym.java. Inside this I call the Demo1 class main()[either int argument or string argument one]. After compiling this Anonym.class file gets generated.
The answer is Yes, but you should consider the following 3 points.
No two main method parameter should be the same
Eg.
public static void main(int i)
public static void main(int i, int j)
public static void main(double j)
public static void main(String[] args)
Java’s actual main method is the one with
(String[] args)
, So the Actual execution starts from public static void main(String[] args), so the main method with(String[] args)
is must in a class unless if it is not a child class.In order for other main methods to execute you need to call them from inside the
(String[] args)
main method.Here is a detailed video about the same: https://www.youtube.com/watch?v=Qlhslsluhg4&feature=youtu.be
Yes, you can have as many main methods as you like. You can have main methods with different signatures from main(String[]) which is called overloading, and the JVM will ignore those main methods.
}