Can there exist two main methods in a Java program?

后端 未结 16 853
后悔当初
后悔当初 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:56

    The below code in file "Locomotive.java" will compile and run successfully, with the execution results showing

    2
    

    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);
        }
    }
    

提交回复
热议问题