← Back to Java

public static void main

JavaEntryjava

The Question

What is the significance of 'public static void main(String[] args)' in Java?

What a Strong Answer Covers

  • entry point for JVM
  • public = accessible outside class
  • static = no object needed
  • void = no return
  • String[] args = command-line arguments

Senior-Level Answer

The `public static void main(String[] args)` signature is Java's program entry point — the method the JVM calls to start execution. Every keyword in the signature is load-bearing.

`public`: The JVM must be able to call this method from outside the class. Making it public gives the JVM (and any external caller) access regardless of the calling context. If it were private or package-private, the JVM runtime would throw a `NoSuchMethodException` or `IllegalAccessException` at launch.

`static`: The JVM must invoke `main` before any instance of the class exists — there is no object to call the method on. `static` means the method belongs to the class itself, not to instances. This allows invocation without first constructing an object, which is necessary because the JVM has no arguments to pass to a constructor at this point.

`void`: The `main` method returns no value to the JVM. Exit status is communicated via `System.exit(int)` if needed. The JVM doesn't inspect a return value from main — void is required by the specification.

`main`: This specific method name is a JVM convention. The JVM looks for a static method named exactly `main` with the `String[]` parameter signature. Other overloads of `main` with different parameter types are valid Java methods but will not be used as entry points.

`String[] args`: Command-line arguments passed by the OS to the JVM are made available as an array of strings. `args[0]` is the first argument after the program name. The array is empty (not null) if no arguments are provided. Since Java 21, a simpler unnamed `main()` method is supported in preview mode (JEP 445) for scripts and educational contexts, relaxing these requirements — but the traditional signature remains the standard for production code.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly explains why each keyword is required (public=access, static=no instance needed, void=no return), and knows that the JVM looks for this specific signature by convention.

3/3 — Strong Answer

Explains what happens at the JVM level when each keyword is wrong (exception types thrown), mentions that String[] args provides command-line arguments and is empty not null by default, and is aware of the Java 21 unnamed main preview feature.

Common Mistakes

  • Saying 'static is needed because main is special' without explaining that no object exists yet to call an instance method on.
  • Not knowing what happens if you omit public or static — NoSuchMethodException or MainMethodNotFound error from the JVM launcher.
  • Claiming args is null when no arguments are provided — it is an empty array, not null.
  • Not knowing that you can name the parameter anything (e.g., String[] arguments) — only the type signature matters for JVM lookup, not the parameter name.

Follow-Up Questions

  • What happens if you define main as private static void main(String[] args)? — The JVM launcher cannot access it — throws MainMethodNotFoundException or IllegalAccessException depending on JVM version. Since Java 9 with module system, this is enforced more strictly.
  • Can you overload main in Java? Will the JVM call the overloaded version? — Yes, you can overload main with different parameters. No, the JVM only calls the specific String[] signature — other overloads are valid Java methods but never used as entry points.
  • How do you access command-line arguments, and what is args[0] when you run 'java MyApp hello world'? — args[0] = 'hello', args[1] = 'world'. The class name is not included in args — it's consumed by the JVM launcher itself.
  • What changed in Java 21 regarding the main method signature? — JEP 445 (preview) allows instance main methods without String[] parameter and without public — the JVM launcher tries multiple signatures. Aimed at simplifying educational and scripting use cases.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • String Constant Pool

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout