Sub topic 1
Why Hello World Needs So Much Ceremony
A first Java program often looks harder than the task it performs. Printing two words requires a class, a method, several keywords, and a library name:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}The program does only one thing, but Java must also describe how the program fits into its larger runtime model.
Prerequisites
This chapter assumes that you know basic Java syntax, including variables, methods, and control flow. You should also know what a class, field, and method are, and be familiar with compiling and running a Java program from the command line.
What each part does
The outer braces define the body of the Hello class. A class is a named unit that holds data and behavior. Java traditionally requires executable methods to belong to a class.
The public keyword on the class makes the class accessible to code outside its package. When the source file is named Hello.java, the public class name must be Hello.
The main method is the traditional starting point for a Java application. The Java launcher looks for a suitable method with this name and signature.
| Part | Meaning |
|---|---|
| `public` | The launcher and other code can access the method |
| `static` | The method belongs to the class, not an object |
| `void` | The method returns no value |
| `String[] args` | An array containing command-line arguments |
System.out.println is another layer of names. System is a Java class. Its out field represents standard output, which is usually the terminal. The println method writes text and then moves to a new line.
The string "Hello, world!" is the only application-specific part of this example. Everything else connects that action to Java's class and launcher rules.
Why `static` was traditionally required
An object is an instance of a class. Before calling an instance method, a program normally creates an object:
class Greeter {
void greet() {
System.out.println("Hello, world!");
}
}The launcher cannot assume which object it should create or what constructor it should call. A static method avoids that problem because it belongs directly to the class. The launcher can call it without first creating a Hello object.
This makes the traditional entry point predictable, but it also forces beginners to learn several ideas before writing their first useful line.
flowchart TD
A[Java launcher] --> B[Find a suitable main method]
B --> C[Load the Hello class]
C --> D[Call static main]
D --> E[Run System.out.println]The newer instance-main and compact-source features reduce this starting ceremony. They let the launcher create an instance when appropriate, and they let small programs omit the surrounding class declaration. The underlying class-based runtime still exists. The source code simply exposes less of it.
Ceremony versus structure
The traditional form is useful when a program needs explicit structure. The class name is visible, the entry point is unambiguous, and the code works with long-established Java tools and conventions.
For a short learning exercise, however, the structure can hide the main idea. A beginner may spend more time asking what static means than understanding output. Java's newer forms separate the amount of source code from the amount of runtime structure. The compiler and launcher supply some details that the programmer does not need to write.
The important distinction is between the code you write and the code the Java platform needs. Traditional Hello World writes both the useful action and much of the startup scaffolding. Compact source files let you focus first on the useful action.
Exercise
Why is the traditional main method declared static?
Show answer
It belongs to the class, so the Java launcher can call it without first creating an object.