What a Java Program Looks Like
In the previous page you got Java running. Now let's look at what the code itself is actually made of. Java is strict and explicit: there's a fixed shape every program has to follow, and the compiler checks that shape before a single line executes.
Here is the smallest complete Java program. Run it:
That's a lot of words to print one line, and unlike Python or JavaScript you can't skip any of them. Every piece is required, so it's worth understanding what each one does. We'll take it apart from the outside in.
Everything Lives in a Class
Java has no "loose" code floating at the top level of a file. All code lives inside a class, declared with the class keyword and a name:
public class Main {
// everything goes in here
}
The opening { and matching closing } mark the body of the class. The name Main is yours to choose, with one important rule: in a normal .java file, a public class must match the filename exactly. A public class Main belongs in Main.java. Get the capitalization wrong and the compiler refuses to build it.
You'll learn what classes really are in the Classes chapter. For now, treat public class Main { ... } as the required wrapper around your code.
The main Method Is the Starting Line
When you run a Java program, the runtime looks for one specific method to begin execution:
public static void main(String[] args) {
// your code starts here
}
This signature is not negotiable. Each word does a job:
public- the runtime can reach it from outside the class.static- it runs without first creating an object of the class.void- it hands nothing back when it finishes.main- the exact name the runtime searches for.String[] args- an array of any command-line arguments passed to the program.
If even one word is off - Static instead of static, String args instead of String[] args - your code may still compile, but launching it fails with Error: Main method not found in class Main. That message almost always means a typo in this line.
Statements End With a Semicolon
A statement is one complete instruction. In Java, every statement ends with a semicolon ;. The compiler uses the semicolon to know where one instruction stops and the next begins - newlines don't matter to it.
Because line breaks are ignored, all three statements could sit on one line and still work - though nobody writes Java that way:
int score = 90; score = score + 5; System.out.println(score);
Forget a semicolon and the compiler stops you before the program runs:
int score = 90
System.out.println(score);
error: ';' expected
int score = 90
^
A missing semicolon is the most common compile error you'll hit as a beginner. When the compiler points at a line, also check the line above it - that's often where the real omission is.
Blocks and Braces
Curly braces { } group statements into a block. A class body is a block, a method body is a block, and the body of an if or a loop is a block. Blocks can nest inside blocks:
Notice the difference from statements: a line that opens a block ends with {, not a semicolon, and you do not put a semicolon after the closing } of a method, class, if, or loop. Mixing this up is a classic early mistake:
public static void main(String[] args) {
System.out.println("hi");
}; // <- this semicolon is unnecessary (and after a class/method, wrong)
The indentation in the examples is purely for humans. Java ignores it, but consistent indentation is how you and everyone else keep track of which { matches which }. Let your editor auto-indent and the braces will line up for you.
Java Is Case-Sensitive
Java treats uppercase and lowercase as completely different. score, Score, and SCORE are three separate names. The same applies to keywords and built-in names:
If you wrote system.out.println (lowercase s) or String Name then tried to use name, the compiler would reject it. A few names that beginners frequently miscapitalize:
System- capitalS.system.out.println(...)won't compile.String- capitalS. It's a class, unlike the lowercase primitive types.void,int,public,static- all lowercase. They're keywords.
Most "cannot find symbol" errors trace back to a casing slip. The error even tells you which symbol it couldn't find - match its capitalization against what you typed.
Putting the Pieces Together
Here's a slightly bigger program that uses everything from this page - a class, the main method, several statements, a nested block, and consistent casing:
Read it top to bottom: the class wraps everything, main is where execution starts, each instruction ends in ;, the if body is grouped in its own block, and every built-in name is capitalized correctly. That's the skeleton of essentially every Java program you'll write.
Next: Comments
You can now read the structure of a Java file. The next thing to add is notes for humans - text the compiler ignores entirely. The next page covers comments: the // single-line form, the /* */ block form, and when a comment actually earns its place in the code.
Frequently Asked Questions
What is the basic syntax of a Java program?
Every Java program lives inside a class, and execution starts in a method with the exact signature public static void main(String[] args). Statements end with a semicolon, and blocks of code are grouped with curly braces { }. A minimal program is public class Main { public static void main(String[] args) { System.out.println("Hi"); } }.
Why does Java need public static void main(String[] args)?
That's the exact entry point the Java runtime looks for. public lets the runtime call it from outside, static means it runs without creating an object first, void means it returns nothing, and String[] args receives command-line arguments. Change any word - even the capitalization - and the program compiles but won't start, with Error: Main method not found.
Do all Java statements need a semicolon?
Every statement ends with a semicolon - variable declarations, assignments, method calls. But lines that end in a block (if, for, while, a class or method header) end with { instead, and you do not put a semicolon after the closing } of those blocks. Forgetting a semicolon is the single most common beginner compile error in Java.