Can there exist two main methods in a Java program

2019-01-16 19:11发布

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.

16条回答
祖国的老花朵
2楼-- · 2019-01-16 20:00

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.

查看更多
唯我独甜
3楼-- · 2019-01-16 20:00

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.

查看更多
We Are One
4楼-- · 2019-01-16 20:03

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.

查看更多
Fickle 薄情
5楼-- · 2019-01-16 20:06

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
查看更多
Fickle 薄情
6楼-- · 2019-01-16 20:06

The answer is Yes, but you should consider the following 3 points.

  1. 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)
  2. 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.

  3. 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

查看更多
看我几分像从前
7楼-- · 2019-01-16 20:07

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.

public class TwoMain { 
public static void main(String args1[]) 
{ 
    System.out.println("First main"); 

} 
public static void main(String args2[]) 
{ 
    System.out.println("Second main"); 



}    

}

查看更多
登录 后发表回答