How JEP 445 Simplifies Java’s Hello World for Beginners
JEP 445 proposes flexible main methods and anonymous main classes to lower Java’s entry barrier, allowing non‑public or non‑static mains, omitting String[] args, and simplifying the classic Hello World example, with usage instructions for JDK 21 preview mode.
OpenJDK JEP 445 aims to simplify Java’s entry point 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 while Java excels at building large, complex applications, its traditional "Hello, World!" program is overly complicated for newcomers, requiring class declarations, public and static modifiers, and a String[] args parameter.
public static void main(String[] args) {
System.out.println("Hello, World!");
}This code includes concepts such as class visibility, static context, and command‑line arguments that are unnecessary for a simple introductory example.
The proposal enhances the Java startup protocol with three key changes:
Allow the main method of the launched class to have public, protected, or package (default) access.
If a class lacks a static main method with a String[] parameter but provides a static main method without parameters, that method will be invoked.
If a class has no static main method but possesses a non‑private zero‑argument constructor and a non‑private instance main method, the class is instantiated and the appropriate main method is called, preferring one with a String[] parameter if present.
These changes permit omission of the String[] args parameter and allow main methods that are neither public nor static, enabling a much simpler "Hello, World!" program:
void main() {
System.out.println("Hello, World!");
}The proposal also introduces an anonymous Main class to implicitly declare the class, further reducing boilerplate:
void main() {
// code here
}This feature is a preview language feature and is disabled by default. To try it in JDK 21, enable preview mode when compiling and running:
javac --release 21 --enable-preview Main.java
java --enable-preview Main
# or
java --source 21 --enable-preview Main.javaFor more details, see the full proposal at https://openjdk.org/jeps/445 .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
