Skip to toolbar

QnA | Can main method be overloaded?

Question: Can main method be overloaded? or Can a java program have multiple methods with name “main” keyword?

Answer: Yes, the Main method can also be overloaded same as normal method overloading techniques. Method overloading is a sub-process of Polymorphism. To achieve method overloading we have to declare and define multiple methods with the same name but having different signatures like the number of parameters, type of parameters type etc.

Main Method Overloading
Main Method Overloading

Note: Keeping only return type different in multiple methods will not be allowed overloading in Java.

Although, we can overload the main method but, the entry point of a Java program will always be the method with default signature as mentioned below:

                                Public static void main(String args[])

We can call other methods by default main method.

Please refer Java | Object Oriented Programming (OOPs) Concept link to get understanding of method overloading.

Code Example:

public class TestMain 
{
	// Default main method which will be entry point of the program.
	public static void main(String[] args) 
	{
		System.out.println("The entry point of the Program.");
		main(10);
		main();
	}
	
	// First instance of the main method.
	public static void main(int x)
	{
		System.out.println("Main method overloading first instance.");
	}
	
	// Second instance of the main method.
	public static void main()
	{
		System.out.println("Main method overloading the second instance.");
	}
	
	// Keeping only return type different will not be allowed overloading in Java.
	/*public static String main(int y)
	{
		System.out.println("Main method overloading second instance.");
		return "Main method overloaded...";
	}*/
}

Output:
The entry point of the Program.
Main method overloading first instance.
Main method overloading the second instance.
Related Links:
		
		Java Tutorials - Basic 		
		
		
		Java - File Handling		
		
		
		Agile		
		
		
		Question & Answer		
		

2682total visits,1visits today

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 11
  • Page views today : 11
  • Total visitors : 48,088
  • Total page view: 66,220

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.