Is the
public static void main
method on the superclass inherited by subclasses? Yes, it can be accessed by all subclasses directly. So it is possible to have a main method just on the superclass, and be able to run each subclass that doesn't itself declare the main
method. For example:public class Main {
public static void main(String[] args) {
System.out.println("In Main.main(String[])");
}
}
public class Main2 extends Main {
public void m2() {
main(null); //access the main method in superclass
}
}
Running Main, Main2, Main3 have the same effect:
public class Main3 extends Main2 {}
C:\tmp> java MainWhen running Main2 and Main3 inside NetBeans 6.1 beta, I got the error
In Main.main(String[])
C:\tmp> java Main2
In Main.main(String[])
C:\tmp> java Main3
In Main.main(String[])
Class "Main2" does not have a main method
. Note this error is not from java runtime. It seems NetBeans is doing some validation and being too restrictive.