Can we have multiple main methods in same class

Can we have multiple main methods in same class

Answer: As long as method parameters (number (or) type) are different, yes we can. It is called method overloading.
Overloaded methods are differentiated by the number and the type of the arguments passed into the method.

Overloaded methods are differentiated by the number and the type of the arguments passed into the method and only main method with single String[] (or) String… as param will be considered as entry point for the program. Example shown below:

package com.javahonk;

public class MainMethodTest {

	public static void main(String[] args) {
		System.out.println("Main method test");
	}

	public static void main(String args) {
		System.out.println("Main method test");
	}

	public static void main(int args) {
		System.out.println("Main method test");
	}

}

 

Leave a Reply

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