The Main Method in Java

Naveen AnamaneniUpdated · 5 min read

A cinema can have many doors, but the ticket says which one to enter by. For a Java program, that door is the main method. When you run java Ticket, the JVM looks for one specific method, with one specific shape, and starts there. Every word in that line has a job.

The Line Everyone Writes

Ticket.java▶ Try it
public class Ticket {
    public static void main(String[] args) {
        System.out.println("Show starts at 6:30 PM");
    }
}
Output
Show starts at 6:30 PM
Every word in the main method has a job

What Each Word Means

WordMeaningWhy it's needed
publicAny code can call this methodThe JVM calls main from outside your class
staticIt belongs to the class, not to an objectWhen the program starts, no objects exist yet, so the JVM needs a method it can call without creating one
voidIt returns nothingThere's nobody to return a value to; use System.exit(code) if you need an exit code
mainThe method's nameThe launcher looks for exactly this name, all lowercase
String[] argsAn array of text valuesHolds the command-line arguments: words typed after the class name

You can also write String... args or String args[], and the name args can be anything, such as String[] words. Those are all the same method to the JVM. But keep the usual form: every Java developer recognizes it instantly.

Command-Line Arguments

Whatever you type after the class name becomes the args array. This program greets a passenger and prints their seat count.

Greet.java▶ Try it
public class Greet {
    public static void main(String[] args) {
        String name = args[0];
        int seats = Integer.parseInt(args[1]);
 
        System.out.println("Namaste, " + name);
        System.out.println("Seats booked: " + seats);
        System.out.println("Arguments received: " + args.length);
    }
}
Output
Namaste, Srihitha
Seats booked: 3
Arguments received: 2

Run it like this:

Terminal
javac Greet.java
java Greet Srihitha 3

How it works

  1. The terminal splits Srihitha 3 at the space, so args[0] is "Srihitha" and args[1] is "3".
  2. Line 4 uses Integer.parseInt to turn the text "3" into the number 3. Arguments always arrive as text, even when they look like numbers.
  3. Line 8 prints args.length, the number of arguments.

To pass a value that contains a space, put it in quotes: java Greet "Srihitha Anamaneni" 3.

Try it yourself: run java Greet Srihitha with only one argument. What happens, and why?

Newer, Shorter Forms (Java 25)

From Java 25, the launcher also accepts simpler entry points: a main that isn't static, isn't public, or takes no parameters at all, such as void main(). These are meant for small programs and learning, and the lesson "Compact Source Files and Instance Main Methods" covers them.

If a class has both, the classic main(String[] args) is chosen first. In interviews and in most existing code, you'll still see the full public static void main(String[] args), so learn that one well.

Common Mistakes

Capital M. public static void Main(String[] args) compiles, but it's a different method. The launcher can't find main and stops with Error: Main method not found in class ....

Making it private. A private main is skipped by the launcher, which gives the same "Main method not found" error.

Treating arguments as numbers. args[1] + 1 with the argument 3 gives "31", because args[1] is text. Convert it with Integer.parseInt first.

Interview Corner

Why is the main method static?

Because the JVM calls main before any object of the class exists. A static method belongs to the class, so the JVM can call it without creating an object first.

Can we overload the main method?

Yes. You can write other methods named main with different parameters, such as main(int n). But the launcher only starts the program from main(String[] args) (or, from Java 25, the simpler forms). The others are ordinary methods you call yourself.

What happens if you run a program without passing any arguments?

args is an empty array with length 0, never null. Reading args[0] throws an ArrayIndexOutOfBoundsException.

Can main return an int, like in C?

No. It must be void. To tell the operating system how the program ended, call System.exit(0) for success or a non-zero number for failure.

Practice

  1. Easy: Write a program that prints all its arguments, one per line. Hint: use args.length and args[0], args[1] and so on, or a loop if you already know one.
  2. Harder: Change Greet so that if no arguments are given, it prints Usage: java Greet <name> <seats> instead of crashing.

Quick check: The main method

4 questions
1Why must main be static?
2You run java Greet Advitha 2. What is args[1]?
3What is args.length when the program runs with no arguments?
4Which of these does the launcher NOT find as the entry point?

Key Takeaways

Was this tutorial helpful?

Lesson 14 of 482