the short answer
The main method is where a Java program starts. When you run a program, Java looks for a method written public static void main(String[] args) and runs the code inside it from top to bottom. Other methods run only when main, or a method that main calls, asks for them. Since Java 25, a shorter void main() is allowed too.
The main method is where a Java program starts. When you run a program, Java looks for this exact line and runs the code inside it, from top to bottom:
public class Main {
public static void main(String[] args) {
System.out.println("Hello from main!");
}
}Hello from main!Five lines just to say hello can look like a lot. This guide explains what every word in that frame is doing, how main hands work to other methods, and what the errors mean when one word is out of place. Every example runs right here in your browser.
The main line, word by word
The line that matters is public static void main(String[] args). Each word has a job:
publicmeans code outside this class is allowed to use the method. Java starts your program from the outside, so main has to be public.staticmeans the method belongs to the class itself, rather than to an object made from the class. Java can call it straight away, before any objects exist.voidmeans the method does not hand back an answer when it finishes. Its job is to do things, not to work out a value.mainis the name Java looks for, with a small m.String[] argsis a list of text values called args, short for arguments. There is more on it below.
The curly brackets after the line mark where main’s code starts and ends. Everything you want the program to do goes between them.
The class around it
In Java, every method lives inside a class. public class Main makes a class called Main, and its curly brackets wrap around everything else, main included.
A public class must be saved in a file with the same name, so public class Main lives in a file called Main.java. Gwecode’s run boxes save your code as Main.java, so this class name will not work here:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}line 1: class Hello is public, should be declared in a file named Hello.javaThe fix is to match the two names, either by renaming the class or, on your own computer, by renaming the file.
What happens when you press run
Before main can run, Java checks and translates your code. On your own computer that happens in two steps. First, javac Main.java compiles the file: it checks every line and turns it into a Main.class file that Java can run. Then java Main starts the Java Virtual Machine, which loads the Main class, finds the main method and calls it.
Since Java 11, java Main.java does both steps in one go for a single file. If the check finds a mistake, nothing runs at all, which is why the errors in this guide appear before any output.
main runs from top to bottom
Java runs main’s lines in order, one after another. Other methods only run when something calls them:
public class Main {
public static void main(String[] args) {
System.out.println("Starting");
greet("Ava");
greet("Ben");
System.out.println("Finished");
}
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}Starting
Hello, Ava!
Hello, Ben!
FinishedThe greet method is written below main, but that does not matter. Java runs it only at the two lines that call it, then carries on in main where it left off. When main reaches its closing bracket, the program ends.
greet is static too. That lets main call it directly, without making an object first. You will meet objects, and methods that are not static, once the basics feel comfortable.
What String[] args is for
args holds any extra words typed after the program’s name when it is started from a command line. On your own computer, starting a program like this:
java Main.java apple pearmakes args hold two strings, apple and pear. Then args.length is 2 and args[0] is apple. Gwecode’s run boxes start programs without any extra words, so args is empty here:
public class Main {
public static void main(String[] args) {
System.out.println("Arguments: " + args.length);
}
}Arguments: 0Many beginner programs never use args at all, but the classic main line still needs it. You may also see it written as String... args or String args[]. Both mean the same thing and both work.
A shorter main in Java 25
Java 25, released in September 2025, made a shorter form official for simple programs:
void main() {
IO.println("Hello from Java 25!");
}There is no class line, no public static and no args. Java wraps the file in a class for you, and IO.println is a new, shorter way to print a line. We ran this with Java 25 to check it.
Gwecode’s run boxes, and most of the Java code you will read, use the classic form, so this guide sticks with it. If you meet the short form in a tutorial, it means the same thing: this is where the program starts.
Common mistakes
A capital M on main
public class Main {
public static void Main(String[] args) {
System.out.println("Hello");
}
}error: no main method found in class Main, please define it as public static void main(String[] args)This code is allowed, because Main is a legal name for a method. But Java looks for main with a small m, finds nothing, and has nowhere to start.
A lowercase string
public class Main {
public static void main(string[] args) {
System.out.println("Hello");
}
}line 2: cannot find symbol
symbol: class string
location: class MainString is the name of a class, and class names in Java start with a capital letter. Java has never heard of a string with a small s.
A missing semicolon
public class Main {
public static void main(String[] args) {
System.out.println("Hello")
}
}line 3: ';' expectedEvery instruction ends with a semicolon. The message names the line, so look at the end of line 3.
A lowercase system
public class Main {
public static void main(String[] args) {
system.out.println("Hello");
}
}line 3: package system does not existIt is System with a capital S. With a small s, Java goes looking for something called system and does not find it.
Leaving out static
Before Java 25, a main method without static makes the program fail to start, with a message saying main is not static. Java 25 accepts it. That difference is one more reason to write the full classic line: it works in every version.
A short exercise
This program should print the lines below, but it has three mistakes. Run it, read the error, fix that one, and repeat. Java shows one problem at a time here, so each fix uncovers the next.
Come on, Leeds!
Come on, York!
Full timepublic class Main {
public static void Main(String[] args) {
cheer("Leeds");
cheer("York");
System.out.println("Full time")
}
static void cheer(string team) {
System.out.println("Come on, " + team + "!");
}
}line 5: ';' expectedTo make your fixes, copy the code into the playground from the button at the bottom of the page and choose Java.
One possible answer
public class Main {
public static void main(String[] args) {
cheer("Leeds");
cheer("York");
System.out.println("Full time");
}
static void cheer(String team) {
System.out.println("Come on, " + team + "!");
}
}Come on, Leeds!
Come on, York!
Full timeThe three fixes: a semicolon at the end of line 5, String with a capital S, and main with a small m.
Related reading
- What does a computer program do?, for the idea of a program running its instructions in order.
- TypeScript vs JavaScript, which also clears up a common mix-up: despite the name, JavaScript is a different language from Java.
Your next step
The main line looks long at first, but after a few programs you will type it without thinking. Gwecode’s Java Level 1 starts with this exact program in its first lesson, then teaches variables, decisions, loops, arrays and methods of your own, one short lesson at a time. Its level check is a tiny quiz game you build yourself. Every lesson is free and runs in your browser. Create a free account to keep your progress, and write your first line below.
practise this · java · lessonYour first linestart the lessonquestions
Why is it called main?
It is simply the name Java looks for, so every program starts in the same place. The idea comes from C, where programs also start in a function called main.
Does every Java class need a main method?
No. Only the class you start the program from needs one. Bigger programs have many classes, and most of them have no main method at all.
What does String[] args do?
It holds any extra words typed after the program’s name when it is started from a command line. Run java Main.java apple pear and args[0] is apple. If nothing extra is typed, the array is empty.
Can I write main without public static void?
From Java 25 you can write a shorter void main() in a simple program. Older versions need the full line, and most code, courses and tutorials still use it, so it is worth knowing well.
What is the difference between main and Main?
Java cares about capital letters. main with a small m is the method Java starts from. Main with a capital M is usually the name of the class. A method called Main is an ordinary method, and Java will not start there.
