I have two sets of code in fist i have defined custom String class and trying to create instance of it while in second i have defined custom System class and trying to create instance of it.
This is neither an interview question nor homework, i just tried it after checking this
In this I am trying to create instance of String class i have defined and i got Exception in thread "main" java.lang.NoSuchMethodError: main
public class Test {
public static void main(String[] args) throws Exception {
String s = new String();
java.lang.System.out.println("done");
}
}
class String {
public String() {
java.lang.System.out.println("custom String");
}
}
In this part i am trying to create instance of System class i have defined and it worked fine
public class Test {
public static void main(String[] args) throws Exception {
System s = new System();
java.lang.System.out.println("done");
}
}
class System {
public System(){
java.lang.System.out.println("custom System");
}
}
Both String and System class are final, so why there is difference in behavior??
This is happening because your
mainis using your customStringclass as argument type which is not matching with standardmainmethod (entry point), which acceptsjava.lang.String[]as parameters; hence complaining.Change your
mainto usejava.lang.Stringclass as: