Java would be enhanced with the ability to succinctly import all packages exported by a module, under a proposal floating in the Java community. The plan would simplify the use of modular libraries in Java.
The preview language feature for Java SE (Standard Edition), called Module Import Declarations, has been filed as a JDK Enhancement Proposal (JEP) in the OpenJDK community.
Goals of the plan include simplifying the reuse of modular libraries by allowing entire modules to be imported at once, avoiding the noise of multiple type-import-on-demand declarations when using diverse parts of the API exported by a module, and allowing beginners to more easily use third-party libraries and fundamental Java classes without having to learn where they are located in a package hierarchy, the proposal states.
It is not a goal to require developers who use the module import feature to modularize their own code. The feature does not require the importing code to be in a module.
Motivating the proposal is the desire to dramatically reduce the number of import declarations that Java programmers need to write. Although classes and interfaces in the java.lang package, such as Object
, String
, and Comparable
, are automatically imported by the Java compiler on demand, other essential classes and interfaces, such as List
, Map
, Stream
, and Path
, are not. As a result, developers must write numerous import declarations at the beginning of every source file.
The ability to import at the level of modules would be especially helpful when APIs in one module have a close relationship with APIs in another module. Automatically importing on demand from transitive dependencies would be a further convenience when prototyping and exploring, the proposal says.
Created in August 2023 and updated April 18, the proposal does not yet target the capability for a specific version of Java. The first possibility would be JDK 23, due in September.