Nugget Friday: Simplify Module Imports in Java 23

Jakarta EE

Welcome to this week’s Friday Nugget! In today’s post, we’re focusing on a new feature coming in Java 23 that’s set to make your coding life a little easier, especially if you’ve ever found yourself frustrated by the clutter of import statements: module import declarations. By leveraging such tool, you can streamline your Java code, reduce verbosity and enhance readability, making your development process more efficient.

The Problem

Java has always been a bit verbose when it comes to importing classes and interfaces. While experienced developers may have grown accustomed to managing numerous import statements at the top of their files, this can be daunting for beginners. Even the wildcard import option (import java.util.*) has its limitations, often leading to cluttered code when diverse parts of a module’s API are used, with multiple import statements at the top of your files. This not only hampers readability but also introduces cognitive noise, making it harder to focus on the logic of the code itself. Moreover, it can be particularly frustrating for beginners who are just starting to explore the Java ecosystem.

Take the following example: 

import java.util.Map;                   

import java.util.function.Function;     

import java.util.stream.Collectors;     

import java.util.stream.Stream;         

String[] fruits = new String[] { "apple", "berry", "citrus" };
Map<String, String> m =
   Stream.of(fruits)
         .collect(Collectors.toMap(s -> s.toUpperCase().substring(0,1),
                                    Function.identity()));

Even though the code is relatively short, the multiple import statements add unnecessary bulk. While import java.util.* could simplify this slightly, it would also pull in everything from the java.util package, much of which might never be used, further cluttering the namespace. This presents a clear challenge: how can we strike a balance between too many specific imports and overly broad wildcard imports? What’s the middle ground?

The Solution

The upcoming Java 23, expected for release in September 2024, introduces a preview language feature called module import declarations (JEP-476) to address this issue. This feature lets you import all the public top-level classes and interfaces from an entire module at once using a single, concise statement.

How Module Import Declarations Work

The new syntax for module import declarations is straightforward:

import module module_name;

This statement will import all the public types from:

  • The packages directly exported by the specified module.
  • The packages exported by modules that are transitively required by the specified module.

This means you can access a wide range of classes and interfaces without the need for multiple import statements, making your code cleaner and easier to read.

Check this example on how you can use the new module import declaration:

import module java.base; // Imports all public types from java.base and its transitive dependencies

// Now you can use List, Map, Stream, Path, etc., without additional import statements
List<String> myList = new ArrayList<>(); 

Benefits of Module Import Declarations

  • Simplified Code: Reduces the number of import statements, leading to cleaner and more readable code.
  • Easier Learning Curve: Helps beginners use third-party libraries and fundamental Java classes without getting bogged down by package hierarchies.
  • Improved Prototyping and Exploration: Simplifies the process of experimenting with new APIs and language features.

Important Considerations

While module import declarations offer significant benefits, there are a few important considerations to keep in mind:

  • Ambiguous Imports: Importing multiple modules could lead to name conflicts if different packages contain types with the same simple name. In such cases, you’ll need to use single-type-import declarations to resolve the ambiguity.
  • Preview Feature: Module import declarations are a preview feature in Java 23 and are disabled by default. You’ll need to enable preview features when compiling and running your code to use them.

Conclusions

The introduction of module import declarations in Java 23 marks a significant step towards simplifying Java development, especially for those new to the language. Module import declarations will offer a convenient way to work with modules and their APIs, promoting cleaner code and a smoother professional development experience, easing the learning curve. While it’s still a preview feature, it shows promising potential for simplifying Java development in the future, leading to a more efficient and accessible programming experience. 

Happy Coding!

 

Comments (0)

Post a comment

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

Payara needs the contact information you provide to us to contact you about our products and services. You may unsubscribe from these communications at any time. For information on how to unsubscribe, as well as our privacy practices and commitment to protecting your privacy, please review our Legal & Privacy Policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Cut Jakarta EE Startup Times from Seconds to Milliseconds with CRaC 8 minutes
Jakarta EE

Cut Jakarta EE Startup Times from Seconds to Milliseconds with CRaC 

Jakarta EE applications can take anywhere from several seconds to over a minute to start, depending on their size […]

Stacked copies of the Payara developer guide “Zero Trust Architecture with Jakarta EE and MicroProfile” on an orange background, showing the dark blue cover design with the Payara logo and a laptop illustration featuring a shield and padlock icon. 4 minutes
Jakarta EE

Implementing Zero Trust Security with Jakarta EE: A Practical Guide

Zero Trust security has moved from buzzword to necessity. The principle is simple: never trust, always verify. But implementing […]

Blue background with coral and fish. Left text: 'MONTHLY CATCH'. Right: laptop screen with tech tabs and Payara Community logo. 4 minutes
Community

The Payara Monthly Catch – December 2025

As we kick off the new year, this January edition of The Monthly Catch looks back at everything that […]