Sub topic 1
Why Hello World needs so much ceremony
Prerequisites
This chapter assumes you can already do the following:
- Read and write basic Java: variables, methods, and control flow.
- Say what a class, a field, and a method are.
- Compile and run a program from the command line with
javacandjava. - Recognize
public static void main(String[] args), even if you have never taken it apart.
You do not need to know anything about Java 25 features yet. This chapter explains the problem those features solve. The next chapters cover the solutions.
The smallest Java program is not small
Here is the traditional first program:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}The program does one thing: it prints a line of text. Yet it contains a class declaration, two access and lifetime modifiers, a return type, a method name, a parameter with an array type, and a call through a static field of another class. A newcomer must accept all of this before seeing any output.
Experienced developers stop noticing this. A teacher or a learner still has to deal with it. The usual advice is "ignore that for now, we'll explain it later". That advice works, but it leaves the learner typing code they do not understand for the first several lessons.
What each piece is for
Each piece of the program exists for a good reason. The reasons only matter once you write larger programs.
| Piece | What it does | Concept it assumes |
|---|---|---|
| `public class Hello` | Declares a class, the unit that holds code. A public class must live in `Hello.java`. | Classes, access control, file naming rules |
| `public` on `main` | Lets the launcher, which is outside your class, call the method. | Access modifiers |
| `static` | Lets the launcher call `main` without first creating a `Hello` object. | The difference between static and instance members |
| `void` | Declares that `main` returns nothing. The launcher ignores any result. | Return types |
| `main` | The agreed name the launcher looks for. | Naming conventions of the platform |
| `String[] args` | Receives command-line arguments, even when the program ignores them. | Arrays, parameters |
| `System.out.println` | Reads the static field `out` of the class `System`, a `PrintStream`, and calls its `println` method. | Static fields, objects, method calls |
Every row of this table needs a concept that beginner courses usually teach later. A first program therefore depends on ideas from several later lessons.
How the launcher treats your main method
The java launcher is the program that starts your code. It does not know your class's design. It only knows a rule for finding an entry point. In the traditional model that rule is rigid. It looks for a method with exactly this shape:
- named
main, - declared
publicandstatic, - returning
void, - taking one parameter of type
String[].
The launcher can call such a method with no object, because static means it belongs to the class itself. This is the real reason static is there. Before the launcher has run any of your code, there is no Hello object. A static method is therefore the only kind it can call.
The public modifier follows the same logic. The launcher is outside your class, so it needs permission to call the method.
The rule is simple and predictable. The cost is that every program must spell out all of these details, even trivial ones.
Common errors with the traditional rule
The rigid rule produces errors that confuse newcomers. These are the ones you will see most often, with their usual causes.
- Wrong file name. Suppose you declare
public class Helloin a file calledhello.javaorMain.java. The compiler stops with a message likeclass Hello is public, should be declared in a file named Hello.java. A public top-level class must live in a file with the same name, including capitalization. - Wrong signature. Writing
public void main(String[] args)withoutstaticused to fail at launch. Before Java 25, the launcher reported an error sayingmainmust be static. The same happened if you wrotestatic void main(String[] args)withoutpublic, orpublic static void main(String args)without the array brackets. The method then does not match the required shape. The code compiles fine, because it is valid Java. The failure appears only when you run it. - Running the wrong thing. Running
java Hello.classfails, becausejavaexpects a class name, not a file name. You writejava Hello. - Missing class in the classpath. Running
java Hellofrom a different directory than the one holdingHello.classfails with a class-not-found error.
Errors 2 and 4 show something worth noticing. The compiler checks that your code is valid Java. It does not check that your code is a valid program. That check happens only when you launch.
Ceremony versus design
It is fair to ask whether the ceremony is a flaw. Part of it is a deliberate design that pays off later.
- Classes as the unit of organization. Putting all code in classes means a small program and a large program use the same structure. Nothing changes when a script becomes an application.
- Explicit access control. Writing
publicandprivatefrom the start builds good habits for code that other people will read. - A single, uniform entry point. Frameworks, build tools, and IDEs all know where to start.
Other parts do not help a beginner at all. A program that takes no arguments must still declare String[] args. A program that never creates objects must still sit inside a class. These parts are there because the language and launcher required them, not because the program needed them.
So there are two separate costs:
- the learning cost, where a beginner needs concepts that are not yet taught,
- the writing cost, where every small script and quick experiment repeats the same boilerplate.
The two costs hit different people. The learning cost affects students and anyone new to Java. The writing cost affects experienced developers who want to test an API or write a quick tool without creating a project.
Comparing with other languages
Other languages show what a lower-ceremony first program looks like.
print("Hello, world!")Python needs no class, no entry-point method, and no import. The program is the single statement. Java's design makes a different trade-off. It asks for structure up front and returns that investment as code grows. The goal of the features in this tutorial is not to turn Java into Python. The goal is to let a small program stay small, and to let the structure arrive when a program needs it.
The direction of the solution
The tutorial's other chapters cover the changes that remove the ceremony one step at a time, without adding a second language. In outline:
- The launcher becomes more flexible about which
mainmethod it selects. It can accept methods that are notstatic, notpublic, or have no parameters. - A source file can contain methods and fields without an enclosing class. The compiler wraps them in a class it declares for you.
- Common packages are imported automatically in such files.
- A small class,
java.lang.IO, provides simple console output and input.
All of this is optional. The traditional Hello program at the start of this chapter still compiles and runs unchanged in Java 25. The new features remove requirements. They do not remove the old way, and they do not change what a full class means.
Self-check before moving on
Before the next chapter, make sure you can explain each of these in your own words:
- why the launcher requires
staticon the traditionalmain, - why a file name must match a public class name,
- why a wrong
mainsignature fails at launch and not at compile time.
If any of these is unclear, reread the sections above. The remaining chapters build on this model by relaxing its rules one at a time.
Exercise
A learner writes the following file and saves it as Greeting.java. It compiles with javac. Using Java 24 or earlier, what happens when they run java Greeting, and why? Name one change that would fix it under the traditional rules.
public class Greeting {
public void main(String[] args) {
System.out.println("Hi!");
}
}
Show answer
Compilation succeeds because the code is valid Java, but running it fails at launch. The traditional launcher requires main to be static and cannot call an instance method before any Greeting object exists. It reports an error saying the main method must be static. The fix is to declare the method as public static void main(String[] args). The failure occurs at launch because the compiler checks the validity of the code, while the launcher checks that the class has the required entry point.
Exercise
Name the two kinds of cost the traditional Hello World imposes, and say who each one mainly affects.
Show answer
The learning cost, where a beginner must understand classes, modifiers, static members, and arrays before seeing output, mainly affects students and newcomers to Java. The writing cost, where every small program repeats the same boilerplate, mainly affects experienced developers who want to write quick scripts or test an idea.