Your first class and the main method
The entry point every Java program needs.
The shape of a program
Every line of Java lives inside a class. Execution starts at a special method called main, which the JVM looks for and calls. Its signature is fixed — memorise it:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Reading it piece by piece:
- public class Hello — declares a class named Hello. A public class must live in a file of the same name: Hello.java.
- public static void main(String[] args) — the entry point. public so the JVM can call it, static so it runs without creating an object, void because it returns nothing, and String[] args receives command-line arguments.
- System.out.println(...) — prints a line to standard output.
Every statement ends with a semicolon; blocks are wrapped in { }.