1) Class
All Java code must live inside a class. Think of a class as the container (or blueprint) for your program.
class Hello {
}
The file name must match the public class name exactly. If the class is Hello, save the file as Hello.java.
2) The main Method
The JVM (Java Virtual Machine) looks for a specific method to start your program: public static void main(String[] args).
public static void main(String[] args) {
}
Why this exact wording? The keywords and parameter list are required so the JVM can find your program’s entry point.
3) Curly Braces { }
Curly braces group code blocks:
- One pair for the class
- One pair for the main method
class Hello { // class begins
public static void main(String[] args) { // main begins
System.out.println("Hello World!"); // statement
} // main ends
} // class ends
Every opening brace { must have a matching closing brace }.
Full Minimal Example
class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}