The whole program#
Hello.javapublic class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Four lines of structure around one line that does something. Every part of that structure is required, and every part has a reason. We will take it from the outside in.
Line 1 — public class Hello {#
Java has no code that lives outside a class. Even a program that only prints one line needs a class to put it in, and this line declares one.
| Word | What it does |
|---|---|
public | Anything may use this class. It also forces the filename — a public class must be in a file of the same name. |
class | The keyword that says a class declaration follows. |
Hello | The name. By convention class names use UpperCamelCase. |
{ | Opens the class body. Everything up to the matching } belongs to this class. |
Save this class into a file called Greeter.java and the compiler stops you immediately:
Greeter.java:1: error: class Hello is public, should be declared in a file named Hello.java
Line 2 — the signature worth memorizing#
public static void main(String[] args) {
This is the line students copy without reading for about a month. It is five separate decisions, and the JVM checks four of them before it will start your program.
| Part | Meaning | Change it and… |
|---|---|---|
public | callable from outside the class — the JVM is outside | the JVM cannot reach it |
static | belongs to the class, not to an object | Main method is not static in class… |
void | returns nothing | Main method must return a value of type void… |
main | the exact name the JVM looks for | Main method not found in class… |
String[] args | an array of command-line arguments | the type must stay String[]; the name is yours |
Why static is the interesting one#
When you run java Hello, no objects exist yet — the program has not started, so nothing has had a chance to create one. A non-static method can only be called on an object. Since there is no object to call it on, the JVM can only start at a method that belongs to the class itself, and that is what static declares. Drop it and you get:
Error: Main method is not static in class Inst, please define the main method as:
public static void main(String[] args)
The name args is not special#
This is worth knowing because it demystifies the line. The parameter name is yours to choose — only the type matters:
public static void main(String[] whatever) {
System.out.println("still works: " + whatever.length);
}
still works: 0
The length is 0 because we passed no command-line arguments. Varargs syntax works too, since it is an array underneath:
public static void main(String... args) { // also a valid entry point
A near miss that compiles#
Capitalize the m and nothing complains until you try to run it:
public static void Main(String[] args) { … }
Error: Main method not found in class Maine, please define the main method as:
public static void main(String[] args)
Main is a perfectly legal method name, so javac has no objection — you have simply written a method nobody calls. This is a good early lesson in the difference between legal and correct.
Line 3 — System.out.println("Hello, world!");#
Three names separated by dots, read left to right:
System— a class built into Java that provides access to the environment the program is running in.out— a field insideSystemholding an object that represents standard output, normally your console window.println— a method on that object which writes its argument and ends the line.
So the dots are not decoration: each one steps into the thing on its left. And Java is entirely case sensitive, which the compiler will demonstrate:
CaseS.java:3: error: cannot find symbol
System.out.PRINTLN("hi");
^
symbol: method PRINTLN(String)
location: variable out of type PrintStream
Read the bottom two lines and the message is doing you a favor: it tells you it was looking for a method called PRINTLN, and where it looked — on a variable named out of type PrintStream.
Braces and semicolons#
Two rules cover almost everything:
- A semicolon ends a statement — one instruction. Declarations and method calls take one;
classand method headers do not. - Braces group statements into a block. Every
{needs its matching}, and the indentation exists purely to help you see the pairing. Java itself ignores whitespace entirely.
Because whitespace means nothing to the compiler, the entire program is legal on one line — and this is exactly why indentation is a discipline rather than a rule. Leave off a semicolon and the caret points at the end of the offending line:
Semi.java:3: error: ';' expected
System.out.println("hi")
^
More than one class in a file#
A .java file may hold several classes. At most one may be public, and that one must match the filename:
public class Outer {
public static void main(String[] args) {
Helper.speak();
}
}
class Helper {
static void speak() {
System.out.println("helper spoke");
}
}
helper spoke
Compiling this produces two class files, Outer.class and Helper.class — one per class, regardless of how many source files they came from.
Comments#
// everything after two slashes, to the end of the line
/* everything between these markers,
however many lines it runs to */
The compiler discards both before it does anything else, so a comment cannot cause an error — and commenting out a line is a legitimate way to isolate which line is causing one.
Where these go wrong#
- Filename and class name drifting apart. Renaming the class in your editor without renaming the file is the usual cause.
- Capitalizing
Main,StringorSystem. Java is case sensitive everywhere, and only some of these are caught at compile time. - Dropping
static. Compiles cleanly, then refuses to start. - Returning a value from
main. Habit carried over from C. Java's entry point returnsvoid. - Unmatched braces. The reported line number is where the compiler gave up, not where you went wrong. Re-indent the file and the missing brace usually becomes visible.
- Believing indentation does something. It communicates to humans and nothing else. Correct-looking indentation over unbalanced braces is a trap.
Test yourself in the free Kestrel Exams app
Topic-selectable practice — offline, no ads, no account.
Practice this topic →Frequently asked questions#
What does public static void main(String[] args) actually mean?
public means the JVM can call it from outside the class. static means it belongs to the class rather than to an object, so it can be called before any object exists. void means it returns nothing. main is the name the JVM looks for. String[] args is an array holding any command-line arguments. Change any of the first four and the program will not start.
Can I rename args to something else?
Yes. args is just a parameter name, so public static void main(String[] cmdLine) works exactly the same. The type String[] is what matters, not the name. Writing String... args instead also works, because varargs are an array underneath.
Why does my file have to have the same name as the class?
Java requires a public class to be declared in a file of exactly the same name, including capitalization. This is how the compiler and class loader locate it. A file may contain other, non-public classes with different names.
Is Java case sensitive?
Completely. System.out.PRINTLN does not compile — it reports “cannot find symbol”, because println and PRINTLN are different names. The same applies to class names, variable names and file names.
What happens if I name the method Main instead of main?
It compiles, because Main is a perfectly legal method name. It then fails to run, with “Main method not found in class …”, because the JVM looks for the exact lowercase name main.
Can one file contain more than one class?
Yes. A .java file can hold several classes, but at most one of them may be public, and that one must match the filename. The compiler produces a separate .class file for each class in the file.
