Fundamentals 6 min read

Understanding Java's main Method: Overloading, Invocation, and Inheritance

This article explains the special nature of Java's main method, detailing its required signature, the role of each modifier, and demonstrates that the main method can be overloaded, invoked by other methods, and even inherited by subclasses, with practical code examples.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Java's main Method: Overloading, Invocation, and Inheritance

When learning Java, the first piece of code you run is the public static void main(String[] args) method, which the JVM calls as the program entry point. This article examines why each keyword is required and how the method works.

The public modifier gives the method the highest visibility so the JVM can access it. static allows the method to be called without creating an instance of its class. The name main follows the convention from the C language, and void indicates the method does not return a value. The String[] args parameter receives command‑line arguments; since Java 1.5 you can also use the var‑args form String... args .

Can the main method be overloaded? Yes. The following example defines two main methods with different signatures; the JVM still uses the standard public static void main(String[] args) as the entry point, while the overloaded version can be called like any other static method.

public class Main {
    public static void main(String args) {
        System.out.println("hello world:" + args);
    }

    public static void main(String[] args) {
        main("test");
    }
}

Can other methods call the main method? Yes. The example below shows a helper method main2 that decrements a counter and then calls main recursively until the counter reaches zero.

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 program prints the decreasing counter values, demonstrating that the entry‑point main can be invoked like any other static method, provided you manage program termination correctly.

Can the main method be inherited? When a superclass defines a public static void main(String[] args) method, a subclass inherits it and can be executed directly, printing the superclass’s output. If the subclass defines its own main , it hides the inherited one.

public class Main {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

public class Main2 extends Main {
    // inherits main from Main
}

// Running Main2 prints "hello world"
public class Main2 extends Main {
    public static void main(String[] args) {
        System.out.println("hello world Main2");
    }
}

// Running Main2 now prints "hello world Main2"

In summary, aside from its special role as the JVM entry point, the main method behaves like any other static method: it can be overloaded, called from other methods, and inherited by subclasses.

JavaInheritancestaticMain methodOverloading
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.