Skip to content
Theory · First program · lesson 7 of 8

The main method

So far we have copied the line public static void main(String[] args) whole, without taking it apart. It is time to learn why each word is there.

A concert starts with its first note: the conductor raises the baton, and the orchestra comes in on the first bar. A program also has a place where execution starts, its entry point. In Java, that is the method main. At launch, the JVM looks for a method with this name in the class and calls it.

JAVA
public class Main {
    public static void main(String[] args) {
        System.out.println("Start"); // Start
    }
}

This is where the concert picture falls short. A concert starts on the first page of the score. A program starts not on the first line of the file but at the method main, wherever it stands in the class.

Word by word

The header of the method main has five parts:

  1. public: the method is open to calls from outside the class. That is how the JVM calls it at launch.
  2. static: the method belongs to the class itself, and calling it needs no object. We take this word apart separately below.
  3. void: the method returns nothing; it runs its instructions and ends without handing back a result.
  4. main: the name by which the JVM finds the entry point. It is written in small letters only.
  5. (String[] args): the strings that can be passed to the program at launch. String[] means "several strings", and args is their name.

Why main needs static

This is a classic interview question: why is main static? To answer it, we need one more picture.

A class is like the blueprint of a house. You can build as many houses from the blueprint as you like, and in Java they are called objects. An ordinary method is an action of a particular house: you can switch the light on in it only once the house is built. A method marked static belongs to the blueprint itself, and you can call it without building a single house.

When a program starts, there are no objects yet. So the JVM calls main on the class directly, and static is exactly what allows it. The interview answer: main is static so that the JVM can call it before a single object has been created.

Since Java 25, main can do without static: the JVM then creates an object of the class first and calls main on it. On Java 21 such a program does not start, so in this course main is always static.

Launch arguments

The words that follow the file name in the launch command land in args. After the command java Main.java Anna Bob, args holds two strings: Anna and Bob. The programs in this theme pass nothing, and args stays empty.

The Java 25 compact source file

Java 25 added a short form for small programs, the compact source file. It declares no class, its main has neither public static nor args, and IO.println does the printing:

JAVA
void main() {
    IO.println("Hello, Java"); // Hello, Java
}

Such a file is also saved, for example, as Main.java and run with the command java Main.java. The compiler creates the class on its own and names it after the file. IO.println prints the same way as System.out.println.

You will meet the compact source file in recent Java tutorials. This course writes programs in the classic form, with a Main class. That code also works on Java 17 and 21, the versions most often found at work and in interviews.

Common mistake

The method name was written with a capital letter:

JAVA
public class Main {
    public static void Main(String[] args) { // ✗
        System.out.println("Start");
    }
}
TEXT
error: can't find main(String[]) or main() method in class: Main

The compiler sees no error: Main is a valid method name, and the compiler cannot know you meant main. The error shows up only at launch: the JVM looks for a method main in small letters and does not find it. Launched in two steps, with java Main, the message reads differently but means the same: Error: Main method not found in class Main. Change Main to main.

Key points

  • main is the entry point: at launch, the JVM looks for the method main in the class and calls it.
  • public opens the method to calls from outside, void means "returns nothing", and String[] args holds the strings from the launch command.
  • main is static so that the JVM can call it on the class while no object has been created yet.
  • The Java 25 compact source file is void main() and IO.println; this course uses the classic form, which also works on Java 17 and 21.