[java] Can we overload the main method in Java?

Can we overload a main() method in Java?

This question is related to java

The answer is


Yes, you can.

The main method in Java is no extra-terrestrial method. Apart from the fact that main() is just like any other method & can be overloaded in a similar manner, JVM always looks for the method signature to launch the program.

  • The normal main method acts as an entry point for the JVM to start the execution of program.

  • We can overload the main method in Java. But the program doesn’t
    execute the overloaded main method when we run your program, we need to call the overloaded main method from the actual main method only.

    // A Java program with overloaded main()
    import java.io.*;     
    public class Test {         
      // Normal main()
      public static void main(String[] args) {
        System.out.println("Hi Geek (from main)");
        Test.main("Geek");
      }     
      // Overloaded main methods
      public static void main(String arg1) {
        System.out.println("Hi, " + arg1);
        Test.main("Dear Geek","My Geek");
      }
      public static void main(String arg1, String arg2) {
        System.out.println("Hi, " + arg1 + ", " + arg2);
      }
    }
    

    Valid variants of main() in Java


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.


Yes According to my point of view, we are able to overload the main method but method overloading that's it. For Example

class main_overload {
    public static void main(int a) {
        System.out.println(a);
    }
    public static void main(String args[]) {
        System.out.println("That's My Main Function");
        main(100);
    }
}

In This Double Back slash Step, I am just calling the main method....


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

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

Yes, you can overload main method in Java. you have to call the overloaded main method from the actual main method.


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.


This is perfectly legal:

public static void main(String[] args) {

}

public static void main(String argv) {
    System.out.println("hello");
}

YES, you can overload main()

But to be clear -- although you can overload main, only the version with the standard signature will be executable as an application from the command line. e.g

public static void main(String a,String... args){
// some code
}
2)public static void main(String[] args){//JVM will call this method to start 
// some code 
}

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.


Yes you can Overload main method but in any class there should be only one method with signature public static void main(string args[]) where your application starts Execution, as we know in any language Execution starts from Main method.

package rh1;

public class someClass 
{

    public static void main(String... args)
    {
        System.out.println("Hello world");

        main("d");
        main(10);
    }
    public static void main(int s)
    {

        System.out.println("Beautiful world");
    }
    public static void main(String s)
    {
        System.out.println("Bye world");
    }
}

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


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