group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
Gaurav Prasad
Gaurav Prasad
Computer Science
14 Jan 2019

Can we overload main() method?

Is it possible?

Soubhik Maji
Soubhik Maji
Computer Science
3mos ago

Yes we can overload main method ,but JVM only use only  public static void main(String[] args) while launching .You can call main methods from inside this , in which then normal overloading rules will come into play 

Puspanjali Jena
Puspanjali Jena
Computer Science
3mos ago

Yes ,it is possible.we can  call  main method by using static block at the time of class loading.

//------------------ 
... class Example{
             Static{
...               s.o.pln("-----");
                   Example.main(new String[0]);
                   S.o.pln("------");
           }
Public static void main(String args[]){
 S.o.pln("-----");
       }
}
//------------------ 

Shruthi Pillai
Shruthi Pillai
Information Technology
3mos ago

Yes, you can overload main method in Java. You have to call the overloaded main method from the actual main method.The normal main  method  acts as an entry point for the JVM to start the program. 


DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
3mos ago

Yes, JVM will call only public static void main(Stringargs[]) method it wont call any user defined method, the user have to explicitly call the overload method. We can overload the main method in java we wont get any compilation error.

public class example{

       public static void main(String[] args){

                System.out.println("Your in main with String[] args");

}

 public static void main(Integer[] args){

                    System.out.println("Your in main with Integer args");

}

}

For the above code the output would be "Your in main with String[] args", but for getting the second main function as your output you need to explicitly call the method.

To call the other main method we need to add the following statement in the main method with string args as argument.

example.main(new Integer[5]);

After writing the above statement you will get both the output.

Gaurav Prasad
Gaurav Prasad
Computer Science
3mos ago

Great efforts guys.