The Main Method in Java
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
public class Ticket {
public static void main(String[] args) {
System.out.println("Show starts at 6:30 PM");
}
}Show starts at 6:30 PMWhat Each Word Means
| Word | Meaning | Why it's needed |
|---|---|---|
public | Any code can call this method | The JVM calls main from outside your class |
static | It belongs to the class, not to an object | When the program starts, no objects exist yet, so the JVM needs a method it can call without creating one |
void | It returns nothing | There's nobody to return a value to; use System.exit(code) if you need an exit code |
main | The method's name | The launcher looks for exactly this name, all lowercase |
String[] args | An array of text values | Holds 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.
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);
}
}Namaste, Srihitha
Seats booked: 3
Arguments received: 2Run it like this:
javac Greet.java
java Greet Srihitha 3How it works
- The terminal splits
Srihitha 3at the space, soargs[0]is"Srihitha"andargs[1]is"3". - Line 4 uses
Integer.parseIntto turn the text"3"into the number3. Arguments always arrive as text, even when they look like numbers. - 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
- Easy: Write a program that prints all its arguments, one per line. Hint: use
args.lengthandargs[0],args[1]and so on, or a loop if you already know one. - Harder: Change
Greetso that if no arguments are given, it printsUsage: java Greet <name> <seats>instead of crashing.
Key Takeaways
Was this tutorial helpful?