Thursday, September 25, 2025
HomeLanguagesJavaGFact 48 | Overloading main() in Java

GFact 48 | Overloading main() in Java

Consider the below Java program.




// 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);
    }
}


Output:

Hi Geek (from main)
Hi, Geek
Hi, Dear Geek, My Geek

Important Points:
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.

Related Articles :
Valid variants of main() in Java
Overload main in C++
Can we Overload or Override static methods in java ?

This article is contributed by Himanshi Gupta. If you like Lazyroar and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Last Updated :
03 Sep, 2018
Like Article
Save Article
Similar Reads
Related Tutorials
RELATED ARTICLES

Most Popular

Dominic
32319 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6682 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6754 POSTS0 COMMENTS
Umr Jansen
6761 POSTS0 COMMENTS