74

Can we overload a main() method in Java?

Manoj Govindan
  • 72,339
  • 21
  • 134
  • 141
Mohan
  • 3,893
  • 9
  • 33
  • 42
  • 5
    Why would you want to overload the main() method. You can create as many classes as you like with a main() if you want different startup behaviour. Just state the main class when you invoke the jar. Or you could just add some extra arg that you test when your main starts that affects what it does. – locka Sep 21 '10 at 10:26

14 Answers14

130

You can overload the main() method, but only public static void main(String[] args) will be used when your class is launched by the JVM. For example:

public class Test {
    public static void main(String[] args) {
        System.out.println("main(String[] args)");
    }

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

    public static void main(String arg1, String arg2) {
        System.out.println("main(String arg1, String arg2)");
    }
}

That will always print main(String[] args) when you run java Test ... from the command line, even if you specify one or two command-line arguments.

You can call the main() method yourself from code, of course - at which point the normal overloading rules will be applied.

EDIT: Note that you can use a varargs signature, as that's equivalent from a JVM standpoint:

public static void main(String... args)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    By the JVM, to be precise - one can call any overloaded version manually. – Péter Török Sep 21 '10 at 10:21
  • Ya it always print main(String[] args) value in console even i passed arguments in command line.Then what is the use of main() overloading. – Mohan Sep 21 '10 at 10:33
  • 1
    @Mohan: The same as any other method. You can call `main()` from elsewhere in your code. – Jon Skeet Sep 21 '10 at 10:35
  • what happens if i my overloaded method has Object[] arg. Where the execution starts from ? public static void main(Object[] obj){....} – Santhosh Dec 07 '12 at 05:45
  • 1
    @SANTHOSH: No, still `String[]`. That's the *only* signature which is recognized as an entry point. – Jon Skeet Dec 07 '12 at 07:39
  • @JonSkeet isn't `String...` (being equivalent to `String[]`) also recognized as an entry point? – vikingsteve Jul 02 '13 at 07:33
  • @vikingsteve: Yes, but I'd call that an equivalent signature, unlike the others being suggested. I'll edit to clarify. – Jon Skeet Jul 02 '13 at 07:40
  • @JonSkeet : reason behind "only public static void main(String[] args) will be used when your class is launched by the JVM." – Java Man Jan 08 '15 at 05:25
  • 1
    @JavaMan: Well that's what the specification says... if you provided a different signature (e.g. one with your own class as one of the parameters) how would the JVM know what arguments to provide? – Jon Skeet Jan 08 '15 at 10:02
  • @JonSkeet : Thanks man!! my pleasure to get answer from you :) – Java Man Jan 08 '15 at 11:44
  • @JonSkeet - Is there any use of overloading main method? Why do we overload the main method if java will always execute main(String[] args)? – Ritesh Puri May 25 '17 at 05:07
  • 1
    @RiteshPuri: I can't see much in the way of a purpose, but equally it would be odd to *prevent* the overloading. Don't forget that the `main` method can be invoked directly from other code as well, so it's not like such an overload would be inaccessible. – Jon Skeet May 25 '17 at 05:39
17

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.

jaskirat Singh
  • 696
  • 1
  • 8
  • 17
JVM
  • 229
  • 1
  • 2
4

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
Shokat
  • 41
  • 4
3

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 
}
Bhaskar13
  • 191
  • 4
  • 14
Jaimin Patel
  • 4,559
  • 3
  • 32
  • 35
2

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

praveen
  • 21
  • 1
2

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.

vikram
  • 21
  • 1
2

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

roottraveller
  • 7,942
  • 7
  • 60
  • 65
2

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
1

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

philant
  • 34,748
  • 11
  • 69
  • 112
praveen
  • 11
  • 1
1

This is perfectly legal:

public static void main(String[] args) {

}

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

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.

1

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

Tharushi Geethma
  • 1,249
  • 15
  • 21
m_prasu
  • 11
  • 3
1

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....

Phonolog
  • 6,321
  • 3
  • 36
  • 64
0

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");
    }
}
Sumanth
  • 1
  • 3