Java - public static void main(String args[ ]) - Explanation

Java - public static void main(String args[ ]) - Explanation - CodingHumans

Almost every Java program you write in college starts with public static void main(String[] args). Most of us type it from memory for years without asking why each word is there. Then an interviewer asks "what happens if you remove static?" and the answer is not obvious.

This post goes through the signature one word at a time, shows what the JVM actually does with it, and runs real programs that break each rule so you can see the exact error. I tested everything on Java 21, and I also mention what changed in Java 25.

What the JVM looks for

When you run java Greet, the launcher loads the class Greet and searches it for one specific method. Before Java 25, the rule was strict: a method named main, that is public and static, returns void, and takes a single String[] parameter. If it finds that method, it calls it and your program starts. If not, it prints an error and stops.

An important detail: a wrong main does not cause a compile error. javac is happy, because a method called main with any signature is just a normal method. The problem shows up only when you try to run the class.

Each keyword, one by one

public

The launcher is code outside your class, so the method must be visible to it. If you drop public, the class still compiles, but running it fails with "Main method not found in class NoPublic". The JVM simply does not treat a non-public method as an entry point.

static

At startup no object of your class exists yet. A static method belongs to the class itself, so the JVM can call Greet.main(...) without first creating a Greet object and without guessing which constructor to use. On Java 21, removing static gives "Main method is not static in class NoStatic".

void

main returns nothing to the JVM. If you want to report success or failure to the operating system, call System.exit(code) instead. Declaring int main gives "Main method must return a value of type void".

main

Just the agreed name. It is an identifier, not a keyword, so you can even have a variable called main. You can also overload it. Only the String[] version is the entry point; the others are ordinary methods.

public class Overload {
    public static void main(String[] args) {
        System.out.println("JVM called main(String[])");
        main(5);   // the other main is just a normal method
    }

    public static void main(int x) {
        System.out.println("main(int) called with " + x);
    }
}
JVM called main(String[])
main(int) called with 5

String[] args

This array holds the command-line arguments, the words you type after the class name. The parameter name is your choice (args is only a convention), and String args[], String[] args and String... args are all accepted. The array is never null. With no arguments you get an empty array of length 0.

public class Greet {
    public static void main(String[] args) {
        System.out.println("Number of arguments: " + args.length);
        for (int i = 0; i < args.length; i++) {
            System.out.println("args[" + i + "] = " + args[i]);
        }
    }
}

Running java Greet and then java Greet Badal "Coding Humans" 42 prints:

Number of arguments: 0
Number of arguments: 3
args[0] = Badal
args[1] = Coding Humans
args[2] = 42

Note two things. The quotes kept "Coding Humans" as one argument. And 42 arrived as the string "42", not a number. Everything in args is a String, so you have to convert it yourself:

public class Sum {
    public static void main(String... args) {
        int total = 0;
        for (String a : args) {
            total += Integer.parseInt(a);   // args are always Strings
        }
        System.out.println("Total = " + total);
    }
}

java Sum 10 20 30 prints Total = 60. java Sum 10 abc throws NumberFormatException: For input string: "abc", so in real code you would validate the input first.

What changed in Java 21 and Java 25

Java 21 added a preview feature that lets a beginner write a program without the class wrapper and without static or args. Java 25 made it a standard feature (JEP 512, "compact source files and instance main methods"). This file is a complete program:

void main() {
    System.out.println("Hello without a class declaration");
}

On Java 21 I had to run it with java --enable-preview --source 21 Hello.java, and it printed "Hello without a class declaration". On Java 25 no flags are needed. When the launcher finds an instance main, it creates the object for you and then calls the method.

This does not replace the classic signature. Existing code, frameworks, interview questions and most online judges still expect public static void main(String[] args), so learn that one first.

Common mistakes

  • Thinking a compile success means the program will run. A wrong signature compiles fine and fails only at launch.
  • Writing String args instead of String[] args. That is a different method, so the launcher reports "Main method not found".
  • Calling instance methods directly from static main. Inside a static method there is no this. Create an object first, or make the helper method static.
  • Reading args[0] without checking the length. With no arguments that line throws ArrayIndexOutOfBoundsException.

Interview questions on this

  • Can main be overloaded? Yes. Only the String[] version is the entry point.
  • Can main be declared final or synchronized? Yes. Neither modifier stops the JVM from calling it.
  • Can a class run without main? A plain application cannot be launched without one. Old tutorials say a static block can print something first, but since Java 7 the launcher checks for main before initialising the class. I tried it on Java 21: the static block never ran and only the "Main method not found" error appeared.
  • Why is args never null? The launcher always passes an array, empty if there were no arguments.

Related: Welcome to Java is a good first program to try this on, and An Introduction to Java and its History explains where the language came from.

Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.

More Java solutions All coding problems MCQ practice

Comments

Answered 0/0Correct 0