BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News JEP 477 Enhances Beginner Experience with Implicitly Declared Classes and Instance Main Methods

JEP 477 Enhances Beginner Experience with Implicitly Declared Classes and Instance Main Methods

JEP 477, Implicitly Declared Classes and Instance Main Methods (Third Preview), has been promoted from its Proposed to Target to Targeted status. This JEP 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 JEP moves forward the September 2022 blog post, Paving the on-ramp, by Brian Goetz, the Java language architect at Oracle. The latest draft of the specification document by Gavin Bierman, a consulting member of the technical staff at Oracle, is open for review by the Java community. 

Java has long been recognized for its capabilities in building large, complex applications. However, its extensive features can be daunting for beginners who are just starting to learn programming. To address this, this JEP has introduced new preview features to simplify the language for new programmers. These features allow beginners to write their first programs without needing to understand complex language constructs designed for larger applications and empower experienced developers to write small programs more succinctly, enhancing their productivity and code readability.

Consider the classic Hello, World! example that is often a beginner's first program:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

With this JEP, the above program can be simplified to:

void main() {
    println("Hello, World!");
}

The proposal introduces several key features designed to simplify Java for beginners while maintaining its robust capabilities. One of the main highlights is the introduction of implicitly declared classes, allowing new programs to be written without explicit class declarations. In this new approach, all methods and fields in a source file are considered part of an implicitly declared class, which extends Object, does not implement interfaces, and cannot be referenced by name in source code. Additionally, the proposal introduces instance main methods, which no longer need to be static or public, and methods without parameters are also recognized as valid program entry points.

With these changes, developers can now write Hello, World! As:

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

Top-level members are interpreted as members of the implicit class, so we can also write the program as:

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

void main() {
    System.out.println(greeting());
}

Or, using a field as: 

String greeting = "Hello, World!";

void main() {
    System.out.println(greeting);
}

Following the initial preview in JDK 21 (JEP 445) and subsequent updates in JDK 22 (JEP 463), the proposal has been refined further based on community feedback. For instance, in this JEP, implicitly declared classes now automatically import the following three static methods from the new java.io.IO class for simple textual I/O:

public static void println(Object obj);
public static void print(Object obj);
public static String readln(String prompt);

This change eliminates the need for the System.out.println incantation, thereby simplifying console output. Consider the following example:

void main() {
    String name = readln("Please enter your name: ");
    print("Pleased to meet you, ");
    println(name);
}

Many other classes declared in the Java API are useful in small programs. They can be imported explicitly at the start of the source file:

import java.util.List;

void main() {
    var authors = List.of("James", "Bill", "Bazlur", "Mike", "Dan", "Gavin");
    for (var name : authors) {
        println(name + ": " + name.length());
    }
}

However, with the JEP 476, Module Import Declarations, implicitly declared classes automatically import all public top-level classes and interfaces from the java.base module, removing the need for explicit import statements for commonly used APIs such as java.util.List. This makes the development process more seamless and reduces the learning curve for new programmers. More details on JEP 476 may be found in this InfoQ news story.

This is a preview language feature, available through the --enable-preview flag with the JDK 23 compiler and runtime. To try the examples above in JDK 23, you must enable the preview features:

  • Compile the program with javac --release 23 --enable-preview Main.java and run it with java --enable-preview Main; or,
  • When using the source code launcher, run the program with java --enable-preview Main.java; or,
  • When using jshell, start it with jshell --enable-preview.

Rather than introducing a separate dialect of Java, this JEP streamlines the declaration process for single-class programs. This approach facilitates a gradual learning curve, allowing beginners to start with simple, concise code and progressively adopt more advanced features as they gain experience. By simplifying syntax and minimizing boilerplate code, Java continues to uphold its reputation as a versatile and powerful programming language suitable for a wide range of applications. This enhancement not only makes Java more accessible to new programmers but also boosts productivity and readability for experienced developers working on smaller projects.

About the Author

Rate this Article

Adoption
Style

BT