Why must the Java main method be declared public static void main with a String array parameter, and what fails if you get one of those wrong?
Every part of the signature is there for a reason, and the launcher is the reason for all of them.
The method has to be public, because the launcher is a piece of code outside your class and it is not allowed to call anything that it cannot see.
The method has to be static, because the launcher starts your program without creating an object. It only has the class, so it can only call a method that belongs to the class.
The method has to return void, because the launcher does not use a result. Your program ends when the method returns.
The parameter has to be an array of String, because that is where the command line arguments arrive.
Here is what happens when you get one of them wrong. If the method is not public, the launcher reports that it cannot find an entry point. If the method is not static, the launcher cannot call it without an object. If the method returns something other than void, the launcher still refuses to start the program, because it has nowhere to put the result.
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Main {
public static void main(String[] args) throws Exception {
Method entry = Main.class.getMethod("main", String[].class);
System.out.println("is public : " + Modifier.isPublic(entry.getModifiers()));
System.out.println("is static : " + Modifier.isStatic(entry.getModifiers()));
System.out.println("returns : " + entry.getReturnType().getName());
}
}
One modern detail is worth knowing. Java 21 allows an entry point that is an ordinary instance method, and it also allows one that takes no arguments at all. The classic shape is still the one that everybody uses and the one that every tool understands, so keep it unless you have a reason not to.
The next question in this topic lists every shape that the launcher accepts.