How JEP 445 Simplifies Java Hello World for Beginners
JEP 445 introduces flexible main methods and anonymous main classes to streamline Java's entry point, removing unnecessary class, public, static, and argument requirements, thereby making the classic Hello, World! program far simpler for students and new developers.
OpenJDK's JEP 445 proposal aims to simplify Java entry by introducing flexible main methods and anonymous main classes, making the language more approachable for students and beginners.
The proposal’s author Ron Pressler notes that traditional Java “Hello, World!” programs are overly complex for newcomers because they require class declarations, public access modifiers, static main methods, and unused parameters.
<code>public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
</code>Pressler argues that the class keyword, public modifier, static keyword, and String[] args are unnecessary for a simple introductory program.
The proposal enhances the Java program launch protocol with three main flexibilities:
Allow the main method of the launched class to have public, protected, or package (default) access.
If the class lacks a static main method with String[] parameters but has a static main method without parameters, invoke that method.
If the class has no static main method but provides a non‑private zero‑argument constructor (public, protected, or package) and a non‑private instance main method, instantiate the class and invoke either the instance main method with String[] parameters or the parameter‑less version.
These changes permit omitting the
String[]parameter and allowing the main method to be non‑public and non‑static, enabling a simplified Hello, World! program:
<code>class HelloWorld {
void main() {
System.out.println("Hello, World!");
}
}
</code>The proposal also introduces an anonymous main class to implicitly declare the class:
<code>void main() {
System.out.println("Hello, World!");
}
</code>While this simplifies the entry program, further simplifications such as shortening
System.out.printlnare left for future JEPs.
These features are preview language capabilities and are disabled by default. To try them in JDK 21, compile with
javac --release 21 --enable-preview Main.javaand run with
java --enable-preview Mainor
java --source 21 --enable-preview Main.java.
More details on the flexible launch protocol and anonymous main classes are available in the JEP 445 specification.
Proposal Goals
Provide a smooth entry to Java for educators to introduce programming concepts gradually.
Help students write concise introductory programs that can be elegantly expanded as their skills grow.
Reduce ceremony for simple programs such as scripts and command‑line utilities.
Avoid a separate beginner‑specific dialect of Java.
Use the same toolchain as production Java programs without introducing a distinct beginner toolchain.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.