Can we overload the main method in Java?

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

Can we overload a main() method in Java?

14条回答
  •  情书的邮戳
    2020-11-30 18:45

    Yes,u can overload main method but the interpreter will always search for the correct main method syntax to begin the execution.. And yes u have to call the overloaded main method with the help of object.

    class Sample{
    public void main(int a,int b){
    System.out.println("The value of a is "  +a);
    }
    public static void main(String args[]){
    System.out.println("We r in main method");
    Sample obj=new Sample();
    obj.main(5,4);
    main(3);
    }
    public static void main(int c){
    System.out.println("The value of c  is"  +c);
    }
    }
    
    The output of the program is:
    We r in main method
    The value of a is 5
    The value of c is 3
    

提交回复
热议问题