Can we overload the main method in Java?

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

Can we overload a main() method in Java?

14条回答
  •  渐次进展
    2020-11-30 18:42

    Yes a main method can be overloaded as other functions can be overloaded.One thing needs to be taken care is that there should be atleast one main function with "String args[] " as arguments .And there can be any number of main functions in your program with different arguments and functionality.Lets understand through a simple example:

    Class A{
    
    public static void main(String[] args)
    {
    System.out.println("This is the main function ");
    A object= new A();
    object.main("Hi this is overloaded function");//Calling the main function
    }
    
    public static void main(String argu)     //duplicate main function
    {
    System.out.println("main(String argu)");
    }
    }
    

    Output: This is the main function
    Hi this is overloaded function

提交回复
热议问题