Can Java’s main Method Be Inherited? Exploring Overloading, Calls, and Inheritance
This article explains the special rules of Java’s main method, its required signature, how it can be overloaded, invoked from other methods, and even inherited by subclasses, while highlighting the differences from regular static methods.
Yesterday a netizen shared an interview question from an Alibaba interview: Can the Java main method be inherited?
When we first learn Java, the first program we run is the main method, which has the following signature:
public static void main(String[] args) {
}The main method is special because the JVM looks for a public static void main(String[] args) entry point to start the application.
Explanation of each keyword:
public : the method must be accessible to the JVM, so it needs the highest visibility.
static : the JVM calls the method without creating an instance, therefore it must be static.
main : the name is inherited from the C language tradition.
void : the entry method does not return a value.
String[] : an array of strings receives command‑line arguments; since Java 1.5 you can also use the var‑args form String... args.
Beyond the JVM‑mandated signature, a main method behaves like any other static method.
Can the main method be overloaded?
Yes. For example:
public class Main {
public static void main(String args) {
System.out.println("hello world:" + args);
}
public static void main(String[] args) {
main("test");
}
}Both overloads compile and run without issues; only the JVM‑specified signature is used as the program entry point.
Can other methods call the main method?
public class Main {
private static int times = 3;
public static void main2(String[] args) {
times--;
main(args);
}
public static void main(String[] args) {
System.out.println("main method execution:" + times);
if (times <= 0) {
System.exit(0);
}
main2(args);
}
}Running this code produces:
main method execution:3
main method execution:2
main method execution:1
main method execution:0Thus the entry main can be invoked like any static method, but you must manage program termination to avoid infinite loops.
Can the main method be inherited?
If a superclass defines a public static void main(String[] args) and a subclass does not declare its own, the subclass inherits the method and can be executed directly:
public class Main {
public static void main(String[] args) {
System.out.println("hello world");
}
}
public class Main2 extends Main {
}Running Main2 prints hello world, showing that the main method is inherited. If the subclass defines its own main, it hides the superclass version.
In summary, apart from the JVM‑required entry signature, the main method behaves like any other static method: it can be overloaded, called from other methods, and even inherited.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
