Reputation: 3566
Can two main methods exist 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?
Upvotes: 43
Views: 147159
Reputation: 994
Case :1 > We have two main method but with "Main" AND "main2" so jvm only a=calling "main" method .
2> Same method but different params, still jvm calling "main(String[] args) " method.
3> Exactly same method but it gives a compile time error as you can not have two same name method in a single class !!!
Hope it will give you a clear picture.
Upvotes: 1
Reputation: 21
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
Upvotes: 2
Reputation: 21
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 {
public static void main(String args){
System.out.println("Print A");
}
public static void main(String [] args){
System.out.println("Print B");
}
public static void main(int garbage){
System.out.println("Print C");
}
public static void main(int i){
System.out.println("Print D")
}
}
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.
Upvotes: 2
Reputation: 21
The below code in file "Locomotive.java" will compile and run successfully, with the execution results showing
2<SPACE>
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))
public class Locomotive {
Locomotive() { main("hi");}
public static void main(String[] args) {
System.out.print("2 ");
}
public static void main(String args) {
System.out.print("3 " + args);
}
}
Upvotes: 0
Reputation: 401
Here you can see that there are 2 public static void main (String args[])
in a single file with the name Test.java
(specifically didn't use the name of file as either of the 2 classes names) and the 2 classes are with the default access specifier.
class Sum {
int add(int a, int b) {
return (a+b);
}
public static void main (String args[]) {
System.out.println(" using Sum class");
Sum a = new Sum();
System.out.println("Sum is :" + a.add(5, 10));
}
public static void main (int i) {
System.out.println(" Using Sum class main function with integer argument");
Sum a = new Sum();
System.out.println("Sum is :" + a.add(20, 10));
}
}
class DefClass {
public static void main (String args[]) {
System.out.println(" using DefClass");
Sum a = new Sum();
System.out.println("Sum is :" + a.add(5, 10));
Sum.main(null);
Sum.main(1);
}
}
When we compile the code Test.java it will generate 2 .class
files (viz Sum.class
and DefClass.class
) and if we run Test.java we cannot run it as it won't find any main class with the name Test. Instead if we do java Sum
or java DefClass
both will give different outputs using different main()
. To use the main method of Sum class we can use the class name Sum.main(null)
or Sum.main(1)//Passing integer value in the DefClass main()
.
In a class scope we can have only one public static void main (String args[])
per class since a static method of a class belongs to a class and not to its objects and is called using its class name. Even if we create multiple objects and call the same static methods using them then the instance of the static method to which these call will refer will be the same.
We can also do the overloading of the main method by passing different set of arguments in the main. The Similar example is provided in the above code but by default the control flow will start with the public static void main (String args[])
of the class file which we have invoked using java classname
. To invoke the main method with other set of arguments we have to explicitly call it from other classes.
Upvotes: 40
Reputation: 95
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.
class Demo1 {
static int a, b;
public static void main(int args) {
System.out.println("Using Demo1 class Main with int arg");
a =30;
b =40;
System.out.println("Product is: "+a*b);
}
public static void main(String[] args) {
System.out.println("Using Demo1 class Main with string arg");
a =10;
b =20;
System.out.println("Product is: "+a*b);
}
}
Output:
Using Demo1 class Main with string arg
Product is: 200
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.
class Demo1 {
static int a, b;
public static void main(int args) {
System.out.println("Using Demo1 class Main with int arg");
a =30;
b =40;
System.out.println("Product is: "+a*b);
}
public static void main(String[] args) {
System.out.println("Using Demo1 class Main with string arg");
a =10;
b =20;
System.out.println("Product is: "+a*b);
}
}
class Anonym{
public static void main(String arg[])
{
Demo1.main(1);
Demo1.main(null);
}
}
Output:
Using Demo1 class Main with int arg
Product is: 1200
Using Demo1 class Main with string arg
Product is: 200
Upvotes: 1
Reputation: 21
The signature of main method must be
public static void main(String[] args)
A class can define multiple methods with the name main. The signature of these methods does not match the signature of the main method. These other methods with different signatures are not considered the "main" method.
Upvotes: 2
Reputation: 1
i have check in java version 1.6.0_32 multiple main method is working but there should one main method like public static void main(String []args) of type signature. Ex is here which i had tested.
public class mainoverload
{
public static void main(String a)
{
System.out.println("\nIts "+a);
}
public static void main(String args[])
{
System.out.println("\nIts public static void main\n");
mainoverload.main("Ankit");
mainoverload.main(15,23);
mainoverload.main(15);
}
public static void main(int a)
{
System.out.println("\nIts "+a);
}
public static void main(int a,int b)
{
System.out.println("\nIts "+a+" "+b);
}
}
Upvotes: -1
Reputation: 2939
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 have public static void main(String[] args)
. See JLS for details.
Upvotes: 6
Reputation: 1
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.
Upvotes: 0
Reputation: 66637
As long as method parameters (number (or) type) are different, yes they can. It is called overloading.
Overloaded methods are differentiated by the number and the type of the arguments passed into the method
public static void main(String[] args)
only main method with single String[]
(or) String...
as param will be considered as entry point for the program.
Upvotes: 36
Reputation: 11
There can be more than one main method in a single program. But JVM will always calls String[] argument main() method. Other method's will act as a Overloaded method. These overloaded method's we have to call explicitly.
Upvotes: 1
Reputation: 111
There can be more than one main method in a single program. Others are Overloaded method. This overloaded method works fine under a single main method
public class MainMultiple{
public static void main(String args[]){
main(122);
main('f');
main("hello java");
}
public static void main(int i){
System.out.println("Overloaded main()"+i);
}
public static void main(char i){
System.out.println("Overloaded main()"+i);
}
public static void main(String str){
System.out.println("Overloaded main()"+str);
}
}
Upvotes: 9
Reputation: 11572
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)
and public static void main(String ... args)
are both used as entry points and are treated as having the same method signature.
Upvotes: 6
Reputation: 425033
The answer is no; there can only one "main" method - where "main" means an entry point you can "run".
You can code overloaded versions as in your example, but they can't be "run".
Upvotes: 1
Reputation: 980
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).
Upvotes: 6