Can we overload main method in java

Previous
Next

In this post, we will see about “Can we overload main method in java”.This is one of the most asked Core java interview questions.

Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method.

Overload main method in java

For example:

When you run above program, you will get below output.

Inside main(String[] args)

As you can see, we have overloaded main method but still, JVM calls the method with signature public static void main(String[] args).

Please note that JVM considers var args public static void main(String…args) same as public static void main(String[] args).

If you want to call overloaded method, then you need to call it from main method with signatute public static void main(String[] args).
For example:

When you run above program, you will get below output.

Inside main(String[] args)
Inside main(Integer args)
Inside main(Integer arr)

As you can see, we have called the overloaded methods from main method with String[] args.

That’s all about “Can we overload main method in java”.

You can go through other interview questions about method overloading and method overriding.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog