Can We Overload main() Method in Java?
The short answer to, can we overload main method in Java is Yes, we can overload the main() method in Java. A Java class can have any number of overloaded main() methods. But the very first thing JVM (Java Virtual Machine) seeks is the original main() method, i.e., public static void main(String[] args) to execute. The class will compile but won't run without the original main() method.
The main method is a special method in Java that acts as an entry point for running any Java program. It always has the same syntax, i.e., public static void main (String[] args). One can only change the name of the String array argument, for example, args as str.
As JVM starts its execution by invoking the main method of the class, so, it must have an exact signature as mentioned above. The following methods are overloaded main method:
Java program:
Output:
Explanation: As we can see in the above example, JVM executes only the original main() method by default and not the overloaded main() method. As a result, we must call the overloaded main methods from the original main method to execute them, which has been discussed in the later half of the article.
How to Overload Main Method in Java?
Method overloading is a feature in Java that allows a class to have more than one method with the same name as long as their parameter declarations are different, i.e., they differ in the number of parameters, parameter type, or both. One of the ways Java supports polymorphism is by method overloading.
Here, we'll see how can we overload main method in java. You can have as many main methods as you wish, but each one must have a different method signature.
In this java program, we'll define the original main method, as well as overloaded main methods, and see which method JVM calls.
Output:
Explanation: In the above example, we can see that there are three different main methods, the standard one that accepts a String array as an argument and the other two that accept an Integer array and double value respectively as arguments. As shown, JVM executes only the original main() method that accepts a String array as an argument and not the overloaded ones.
How We Can Invoke the Overloaded main() Method?
As we know, the public static void main(String[] args) is the entry point for the JVM to start its execution. So, to invoke the overloaded main() methods, we need to call it from the original main() method.
In the following example, there are three different main methods, and we'll invoke other overloaded main methods from the original one to execute.
Output:
Explanation: In the above example, JVM will, by default, call the original method, but it will also call overloaded "main() method2", which receives a String value as an argument, as defined. A call to overloaded "main() method1" is defined in overloaded "main() method2", which accepts two int values as inputs and prints the sum of two numbers.
Conclusion
- The answer to the question, can we overload the main method in Java is Yes, we can overload as many main methods as we want, provided that the method signature for each main should be different.
- The entry point for the JVM to begin its execution is "public static void main(String[] args)".
- Without the original main() method, the class will compile but won't execute.
- The overloaded main methods won't execute on their own, we need to call it from the actual original method only.