Javatpoint Logo
Javatpoint Logo

Can We Overload main() Method in Java

When we talk about the method overloading in Java, a question arises that can we overload main() method in Java. The answer is, yes, we can overload main() method in Java.

In this section, we are going to learn how can we overload main() method in Java. In short, first, we will understand what is method overloading.

Method Overloading

Method overloading in Java is a feature that allows a class to have more than one method with the same name. To identify each method uniquely, we differentiate each method by types of arguments or the number of arguments or order of arguments.

For example, consider the following:

The above three methods have the same name sum(). The first, sum() method contains two arguments a and b of type int. The second sum() method also contains the two arguments a and b but the type of arguments (double) differentiate it from the other two methods. The third sum() method contains the three arguments a, b, and c of type int, it differentiates itself from the other two methods.

Now move to the point, how can we overload the main method in Java.

Java main() Method Overloading

The main() method is the starting point of any Java program. The JVM starts the execution of any Java program form the main() method. Without the main() method, JVM will not execute the program.

Syntax:

It is a default signature that is predefined in the JVM. It is called by JVM to execute a program line by line and ends the execution after completion of the method. Here a question arises that like the other methods in Java, can we also overload the main() method.

The answer is, yes, we can overload the main() method. But remember that the JVM always calls the original main() method. It does not call the overloaded main() method.

Let's understand the concept through an example.

MainMethodOverload1.java

Output:

Original main() method invoked

When we execute the above example, it always invokes the original main() method not the overloaded main() method. It is because the JVM executes the original main() method by default.

Now the question arises that how we can invoke the overloaded main() method. To call the overloaded main() method, we must call it from the original main() method.

Let's understand it through an example.

MainMethodOverload2.java

Output:

Overloaded main() method invoked

In the above example, there are two main() methods. The first main() method is the original one and the second main() method is the overloaded main() method. It does not parse any parameter.

Remember: If we want to execute the overloaded main() method, it must be called from the original main() method, as we have done in the above example.

Let's see another example.

MainMethodOverload2.java

Output:

Original main() method executed
Hello
First overloaded main() method executed
true
Second overloaded main() method executed
mango
Third overloaded main() method executed
112

Next TopicJava Tutorial




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA