Here you will get answer for very basic question asked by beginners i.e. can we overload main method in Java?
A function is overloaded in java by changing the number of arguments or their type. The main method can also be overloaded in the same way we overload any other method.
How Can We Overload main Method in Java?
Take below example.
public class OverloadMainMethod { public static void main(String args[]) { System.out.println("String args[]"); } public static void main(int a) { System.out.println("int a"); } public static void main(int a, int b) { System.out.println("int a, int b"); } }
Output
String args[]
In above program there are 3 overloaded main methods. If you will run the program it will give above output. The JVM always looks for main method with following signature.
public static void main(String args[]) { //some code here }
The main method acts as an entry point for program execution. Even though we have several main methods in program but JVM will still call main method having above signature.
You can call remaining overloaded main methods in the same way you call any overloaded method.
Comment below if you have doubts or found anything missing in above tutorial.