Friends of OpenJDK Today

Exploring New Features in JDK 23: Simplifying Java with Module Import Declarations with JEP 476

June 20, 2024

Author(s)

  • Avatar photo
    A N M Bazlur Rahman

    A N M Bazlur Rahman is a Software Engineer with over a decade of specialized experience in Java and related technologies. His expertise has been formally recognized through the prestigious ... Learn more

As Java evolves, simplifying code and improving developer productivity remain priorities.

JEP 476 introduces a new feature in JDK 23: Module Import Declarations.

This feature aims to streamline the process of importing multiple packages from a module, enhancing code readability and reducing boilerplate.

What is JEP 476?

JEP 476 proposes the ability to import all packages exported by a module with a single declaration. This is particularly useful for developers who frequently use multiple packages from the same module, as it eliminates the need for numerous individual import statements.

Key Features

  • Simplified Imports: Instead of multiple import statements, a single import module statement can be used. For example, import module java.base; will import all public top-level classes and interfaces from the java.base module, which includes packages like java.util and java.nio.file.
  • Beginner-Friendly: This feature makes it easier for beginners to use third-party libraries and fundamental Java classes without having to learn the package hierarchy by reducing the complexity of import statements.

Usage Example

Consider a scenario where you need to use multiple classes from the java.util package. Traditionally, you would write:

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

With JEP 476, this can be simplified to:

import module java.base;

This single line imports all necessary classes, making the code cleaner and more concise. Consider the following example:

import module java.base;

void main() {


    println("Hello, World!");
    println("Your Locale: " + Locale.getDefault());
    println("A Big Number: " + new BigInteger("12345678901234567890"));

    var random = new Random();
    println("Random Number: " + random.nextInt(100));

    var now = Instant.now();
    println("Current Time: " + now);

    var greetings = new ArrayList<>();
    greetings.add("Hello, World!");
    greetings.add("স্বাগতম বিশ্ব!");
    greetings.add("مرحبا بالعالم!");
    println("Greetings: " + greetings);

    try {
        var localhost = InetAddress.getLocalHost();
        println("Local Hostname: " + localhost.getHostName());
    } catch (Exception e) {
        println("Could not get hostname: " + e.getMessage());
    }

    println("UTF-8 Charset: " + Charset.forName("UTF-8"));
    println("Current Thread: " + Thread.currentThread().getName());
}

//java --enable-preview --source 23 Helloworld.java


With this module import, you import the entire module; there is no need to maintain a long list of import statements at the beginning of the file.

NOTE: 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.

Addressing Ambiguities

One potential issue with module imports is name ambiguity. For example, importing both java.base and java.sql modules might lead to conflicts with classes like Date present in both packages.

In such cases, specific import statements can be used to resolve ambiguities.

Conclusion

JEP 476 represents a significant step towards simplifying Java programming by reducing boilerplate code and improving readability.  More about this JEP can be found in this infoQ news

Related Articles

View All

Author(s)

  • Avatar photo
    A N M Bazlur Rahman

    A N M Bazlur Rahman is a Software Engineer with over a decade of specialized experience in Java and related technologies. His expertise has been formally recognized through the prestigious ... Learn more

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard