Can we overload main method in java?

Interview Question – Can we overload main method in java programs, for example in below class Sample, public static void main(String[] args) is given, that is the start point of the program. So, can we write more methods with same name i.e. main (method overloading)?

 

 

Answer:
Yes, we can overload main method in java program. We know that in method overloading the parameters type or number of parameters and return type should be different.

Hence, any method with same name as main () method with different parameters type or number of parameters and return type can be overloaded.

As an example, below class contains multiple overloaded main() methods.

 

 

After answering the above question, the next question asked is:

If a class has multiple main() method then how JVM decides which main method to call?

Answer: JVM calls only public static void main(String[] args) method and don’t call any user defined overloaded main method.

 

NOTE: We cannot override main method because it is static. In fact, in java no static method can be overridden as they are not part of an object of the class.