Can we overload the main method in Java?

后端 未结 14 1855
不知归路
不知归路 2020-11-30 18:07

Can we overload a main() method in Java?

14条回答
  •  执笔经年
    2020-11-30 18:44

    Yes, by method overloading. You can have any number of main methods in a class by method overloading. Let's see the simple example:

    class Simple{  
      public static void main(int a){  
      System.out.println(a);  
      }  
    
      public static void main(String args[]){  
      System.out.println("main() method invoked");  
      main(10);  
      }  
    }  
    

    It will give the following output:

    main() method invoked
    10
    

提交回复
热议问题