Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Breaking down Barriers: Introducing JDK 21’s Approach to Beginner-Friendly Java Programming

Breaking down Barriers: Introducing JDK 21’s Approach to Beginner-Friendly Java Programming

This item in japanese

JEP 445, Unnamed Classes and Instance Main Methods (Preview), has been promoted from its Proposed to Target to Targeted status. This feature JEP, formerly entitled Implicit Classes and Enhanced Main Methods (Preview), proposes to "evolve the Java language so that students can write their first programs without needing to understand language features designed for large programs." This is a preview language feature.

This JDK Enhancement Proposal (JEP) aims to make Java more approachable for beginners and is led by Brian Goetz, a renowned engineer at Oracle Corporation and the Java language architect. Goetz has detailed the context of this proposal on the OpenJDK website, titled Paving the on-ramp, acknowledging that while Java is a widely taught language and appreciated for its simplicity, it does present some initial challenges to newcomers. Specifically, beginners often find the necessity of declaring a class and understanding the "public static void main" method to be somewhat challenging concepts to grasp.

The proposal puts forth three significant changes to address these concerns: the introduction of a more forgiving launch protocol; the inclusion of unnamed classes; and the establishment of predefined static imports for vital methods and fields. These changes are expected to simplify the learning process and make Java a more welcoming language for those embarking on their programming journey​ in jshell or Notepad or a full-fledged IDE.

This adjustment allows the classic "Hello, World!" program to be simplified to:

class HelloWorld { 
    void main() { 
        System.out.println("Hello, World!");

The second change makes the class declaration implicit. This further simplifies the "Hello, World!" program to:

void main() {
    System.out.println("Hello, World!");

The third change is not demonstrated in the above snippets but involves predefined static imports for essential methods and fields.

As a preview feature, JEP 445 needs to be explicitly enabled when using JDK 21. Developers can do so by compiling the program with the --enable-preview flag:

javac --release 21 --enable-preview

And then running the program with the --enable-preview flag:

java --enable-preview Main

Alternatively, if developers prefer to use the source code launcher, they can run the program with the --enable-preview flag:

Developers should remember to replace 'Main' in the above commands with the actual name of their Java file or class.

The Java Language Specification (JLS) is being updated with a more flexible launch protocol to offer greater flexibility in declaring a program's entry point and to allow instance main methods. This protocol enables the main method of a launched class to have public, protected, or default access, and it also provides support for static main methods with no parameters. If a class doesn't have a static main method but contains a non-private zero-parameter constructor and a non-private instance main method, then an instance of the class can be constructed, and the instance main method can be invoked. This flexibility allows programs, such as "Hello, World!", to be written without access modifiers, static modifiers or a String[] parameter. Also, it issues a warning at runtime if there is a change in behavior due to the invocation of an instance main instead of an inherited "legacy" main method.

Java is now introducing the concept of unnamed classes to further simplify the language for beginners and small programs. These unnamed classes, always part of the unnamed package, are helpful for standalone programs or program entry points. When the Java compiler encounters a method not enclosed in a class declaration, it will consider such methods, any unenclosed fields, and classes declared in the file as members of an unnamed top-level class. Unnamed classes are final, cannot implement any interface or extend any class other than Object, and cannot be referenced by name. However, they must contain a main method that can be launched, a requirement enforced by the Java compiler.

This new feature allows developers to write programs without explicit class declarations. For example, "Hello, World!" can be written just as a method or using a field, with top-level members interpreted as part of the unnamed class. If an unnamed class has an instance main method, launching it is equivalent to employing the existing anonymous class declaration construct. Moreover, a source file containing an unnamed class can be launched with the source-code launcher, with the Java compiler compiling the file into a launchable class file. So developers can write the program as:

String greeting() { return "Hello, World!"; }

void main() {

or, using a field as:

String greeting = "Hello, World!";

void main() {

This simplification enhances Java's flexibility and ease of use, especially for beginners still getting comfortable with core programming concepts.

Developers who want to experiment with these new features can download the OpenJDK from the OpenJDK JDK 21 Early-Access Builds.

Another alternative is to use SDKMan, a software development kit manager, to download and manage different versions of Java. SDKMan can be used via the command line, which can make the process easier for developers who prefer this method.

However, these are early-access builds, so they may not be as stable as the final release, scheduled for September 2023, and are intended for testing and feedback purposes.

About the Author

Rate this Article