Yes,We can overload main method in a
java class. But in order to run a class the JVM will look for
public static void main(String[] args){
}
only. Otherwise it will show an error as "java.lang.NoSuchMethodError: main"
That is the execution will start only in this method. For example
public class MainExample {
public static void main(String[] args) {
MainExample me=new MainExample();
System.out.println("Test");
main(4);
System.out.println(me.main("Main method example"));
}
public static void main(int k){
System.out.println("test 5");
}
String main(String s){
return s;
}
}
This class will compile and give the output as
Test
test 5
Main method example