The <code>main</code> Method in Java β€” <code>public static void main(String[] args)</code>

public static void main(String[] args) is the entry point the JVM looks for when you run java ClassName. If it's missing or has the wrong signature, the JVM refuses to start:

Error: Main method not found in class ...

The canonical signature

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

Why each keyword is there

  • public β€” the JVM (outside your class) needs to call it.
  • static β€” no instance exists yet when the JVM starts.
  • void β€” the JVM doesn't expect a return value. Use System.exit(code) to set a non-zero exit code.
  • main β€” the fixed, case-sensitive name.
  • String[] args β€” command-line arguments, not including the program name.

Accepted variants

public static void main(String args[])          // old-style array syntax
public static void main(String... args)          // varargs β€” compiles down the same

Command-line arguments

public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("Usage: program <file>");
        System.exit(1);
    }
    var path = Path.of(args[0]);
    ...
}

Multiple main methods

Any class can have a main method. The JVM runs the one in the class you name on the command line. This is handy for quick debugging β€” give each class its own main as a scratch pad.

Simplified main (Java 21+ preview, finalised in Java 25)

void main() {
    System.out.println("Hello!");
}

No public static, no String[] args. Top-level methods without a class wrapper are also allowed in preview. This lowers the barrier for beginners β€” but for production code, stick to the classic form.

Common mistakes

  • Typo in the signature β€” Main, Void, String arg[], missing static. The JVM is unforgiving.
  • Reading args[0] without checking length β€” ArrayIndexOutOfBoundsException on no args.
  • Catching everything in main β€” the default uncaught-exception handler prints a clean stack trace. Don't swallow it.

Related

Pillar: Java methods. Run any main in the Java Online Compiler.