Can there exist two main methods in a Java program?

后端 未结 16 628
后悔当初
后悔当初 2020-12-08 02:21

Can there exist two main methods in a Java program?

Only by the difference in their arguments like:

public static void main(String[] args)

相关标签:
16条回答
  • 2020-12-08 02:53

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

    0 讨论(0)
  • 2020-12-08 02:56

    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.

    0 讨论(0)
  • 2020-12-08 02:56

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:57

    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.

    0 讨论(0)
  • 2020-12-08 02:59

    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
    
    0 讨论(0)
  • 2020-12-08 03:01

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

    0 讨论(0)
提交回复
热议问题