Can we overload the main method in Java?

后端 未结 14 1800
不知归路
不知归路 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
    
    0 讨论(0)
  • 2020-11-30 18:45

    yes we can overload main method. main method must not be static main method.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-30 18:48

    Yes, you can overload main method in Java. But the program doesn't execute the overloaded main method when you run your program, you have to call the overloaded main method from the actual main method.

    that means main method acts as an entry point for the java interpreter to start the execute of the application. where as a loaded main need to be called from main.

    0 讨论(0)
  • 2020-11-30 18:52

    Yes, main method can be overloaded. Overloaded main method has to be called from inside the "public static void main(String args[])" as this is the entry point when the class is launched by the JVM. Also overloaded main method can have any qualifier as a normal method have.

    0 讨论(0)
  • 2020-11-30 18:53

    Yes. 'main( )' method can be overloaded. I have tried to put in some piece of code to answer your question.

    public class Test{
    static public void main( String [] args )
            {
                    System.out.println( "In the JVMs static main" );
                    main( 5, 6, 7 );    //Calling overloaded static main method
                    Test t = new Test( );
                    String [] message  = { "Subhash", "Loves", "Programming" };
                    t.main(5);
                    t.main( 6, message );
            }
    
            public static void main( int ... args )
            {
                    System.out.println( "In the static main called by JVM's main" );
                    for( int val : args )
                    {
                            System.out.println( val );
                    }
            }
    
            public void main( int x )
            {
                    System.out.println( "1: In the overloaded  non-static main with int with value " + x );
            }
    
            public void main( int x, String [] args )
            {
                    System.out.println( "2: In the overloaded  non-static main with int with value " + x );
                    for ( String val : args )
                    {
                            System.out.println( val );
                    }
            }
    }
    

    Output:

    $ java Test
    In the JVMs static main
    In the static main called by JVM's main
    5
    6
    7
    1: In the overloaded  non-static main with int with value 5
    2: In the overloaded  non-static main with int with value 6
    Subhash
    Loves
    Programming
    $
    

    In the above code, both static-method as well as a non-static version of main methods are overloaded for demonstration purpose. Note that, by writing JVMs main, I mean to say, it is the main method that JVM uses first to execute a program.

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