Skip to content

The main signature, word by word

โ˜• Java 17 ยท Writing Your First main() Method ยท 1/3

The front door of your program

When you run java StreamingApp, the JVM looks inside StreamingApp for one specific method and starts there: main.

public class StreamingApp {
    public static void main(String[] args) {
        // the program starts here
        System.out.println("Netflix is loading...");
    }
}

In Java, everything lives inside a class, including the entry point. If the JVM finds no valid main, the program doesn't start.

Each word has a job

public static void main(String[] args) { ... }
Word Meaning Why the JVM needs it
public visible from outside the class the JVM calls it from outside
static belongs to the class, not to an object no object exists yet when the program starts
void returns nothing when main ends, the program ends
main the name the JVM looks for lowercase, exactly
String[] args an array of String receives the words typed after the class name

Change any of these incorrectly and the JVM no longer recognizes the method as the entry point.

Valid variations

Java is flexible on a few details. All of these work:

public static void main(String[] args) { }          // the classic form: use this one
public static void main(String[] arguments) { }     // the parameter name is free
public static void main(String... args) { }         // varargs: same as String[]
static public void main(String[] args) { }          // public and static in any order
public static final void main(String[] args) { }    // final is allowed

What must stay fixed:

  • the method is public and static,
  • it returns void,
  • it's named main, in lowercase,
  • it takes one String[] or String... parameter.

String... is varargs syntax: for the JVM it's the same as an array of String.

You know what a valid entry point looks like. Next: the mistakes that silently break it, and how to use args.

๐Ÿ’ป Code: Java17_Part_1_Fundamentals_Core_Syntax on GitHub

Next: what breaks main โ†’