Sub topic 3
The `import module` Declaration
This chapter assumes that you can write and compile basic Java classes, use single-type imports such as import java.util.List;, and use on-demand imports such as import java.util.*;. You should also know that a fully qualified name includes its package, for example java.util.List.
Why module imports exist
A traditional on-demand import exposes the types in one package:
import java.util.*;A module can contain many packages. Importing every package separately can make a source file long:
import java.util.*;
import java.util.stream.*;
import java.time.*;Java provides a module import declaration for this situation:
import module java.base;The declaration imports types from the module rather than from one package. It is a compile-time naming feature. It does not add a module dependency by itself.
Basic syntax
The syntax is:
import module module.name;The module name must identify a module available to the compiler. For example, java.base is the module containing the Java Collections Framework, many language-support classes, and the stream API.
The following program uses several types from different packages in java.base:
import module java.base;
public class ModuleImportDemo {
public static void main(String[] args) {
List<String> names = List.of("Ada", "Grace", "Alan");
Map<String, Integer> lengths = new HashMap<>();
for (String name : names) {
lengths.put(name, name.length());
}
String joined = names.stream()
.sorted()
.collect(Collectors.joining(", "));
System.out.println(joined);
System.out.println(lengths.get("Grace"));
}
}Without the module import, you would need imports for List, Map, HashMap, and Collectors, or package-level on-demand imports.
What the declaration makes visible
A module import makes accessible public top-level types from the packages that the module exports. A public top-level type is a public class, interface, enum, record, or annotation type declared directly in a package.
It does not make every name inside the module visible. In particular, it does not expose:
- Types from packages that the module does not export.
- Non-public top-level types.
- Members such as methods and fields.
- Types that are inaccessible because of module boundaries.
The declaration also does not import static members. You still need a static import when you want to use a static method or constant without its type name.
| Declaration | Main scope of names |
|---|---|
| `import java.util.List;` | One named type |
| `import java.util.*;` | Accessible types in one package |
| `import module java.base;` | Accessible public top-level types in exported packages of one module |
A module import is therefore broader than a package wildcard import, but it still follows Java's access rules.
Module readability still matters
Java has two related ideas:
- An
import moduledeclaration controls which type names can be used without qualification. - A module's
requiresdirective controls whether a named module reads another module.
For code in a named module, the enclosing module must read the imported module. For example:
module com.example.app {
requires java.sql;
}A source file in com.example.app can then write:
import module java.sql;The import declaration does not replace requires java.sql;. The first affects source-level name lookup. The second establishes the module relationship needed for access.
When the declaration is useful
Use a module import when a source file intentionally uses many types from several exported packages in the same module. It can make small examples and compact demonstrations easier to read.
Prefer explicit imports when a production file uses only a few types or when readers benefit from seeing each dependency clearly. A module import can hide where a type comes from. It can also make a newly added dependency create a name conflict.
For example, importing both java.base and java.sql can make the simple name Date unclear because both modules expose a type with that name. You can resolve such a conflict by using a single-type import or a fully qualified name.
Version and compilation
Module import declarations were preview functionality in JDK 23 and JDK 24. They became a permanent feature in JDK 25.
On JDK 25 or later, compile and run the example normally:
javac --release 25 ModuleImportDemo.java
java ModuleImportDemoOn JDK 23, enable the preview feature during both compilation and execution:
javac --release 23 --enable-preview ModuleImportDemo.java
java --enable-preview ModuleImportDemoThe --release value and the runtime preview setting must match the JDK version used for the preview feature.
Exercise
Does import module java.base; make a named module depend on java.base?
Show answer
No. It makes exported public top-level types available by simple name during compilation. A named module's dependency is declared separately with requires java.base;, although every named module implicitly requires java.base.