Sub topic 11
When to Use Module Imports and When Not To
Assume you can write and compile basic Java classes, understand packages, and know single-type imports such as import java.util.List; and on-demand imports such as import java.util.*;. You should also know that a module can export packages and can read another module through requires.
A module import declaration lets a source file use public top-level types exported by a module without naming each package:
import module java.base;This can replace several ordinary imports when a file uses many types from the same module. It changes name lookup in the source file. It does not add a module dependency or change which packages the module exports.
The readability trade-off
Use a module import when the source file uses many unrelated types from one stable, well-known module. The declaration keeps the import section short:
import module java.base;
public class Report {
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 result = names.stream()
.sorted()
.collect(Collectors.joining(", "));
System.out.println(result);
System.out.println(lengths);
}
}Here, List, Map, HashMap, and Collectors come from packages exported by java.base. An explicit list would be more verbose, but it would also show exactly where every type comes from.
A module import is less helpful when a file uses only one or two types. In that case, explicit imports communicate the dependencies more clearly:
import java.time.LocalDate;
import java.util.List;The following table gives a practical rule of thumb.
| Situation | Better choice | Reason |
|---|---|---|
| Many types from one familiar module | Module import | Reduces repetitive declarations |
| One or two important types | Single-type imports | Makes dependencies obvious |
| Several modules with overlapping names | Explicit imports | Prevents or documents ambiguity |
| Public library code with a stable API | Usually explicit imports | Keeps source intent clear to readers |
Prefer explicit imports at important boundaries
Explicit imports are often better in public examples, shared libraries, and code that reviewers must understand quickly. They make a type's package visible without requiring the reader to inspect the module declaration.
They also reduce surprises when a dependency adds a new exported type with a name that your code already uses. Module contents can grow over time. A broader import can therefore create a new ambiguity after a library or JDK upgrade.
A module import does not automatically make every type usable. Only public top-level types in packages exported by that module are candidates. Package-private types, non-exported packages, and types blocked by normal access rules remain unavailable.
Understand ambiguity before choosing convenience
Importing multiple modules can make a simple name ambiguous:
import module java.base;
import module java.sql;
public class Dates {
public static void main(String[] args) {
// Date date = null;
// Compile error: java.util.Date and java.sql.Date both match.
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate =
new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);
}
}The fully qualified name includes the package and type name. Use it when both types are genuinely needed. Otherwise, use a single-type import to select one type:
import module java.base;
import module java.sql;
import java.util.Date;
public class ResolvedDate {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now.getTime() > 0);
}
}A local declaration has an even stronger claim. If your source file defines a type named Date, that local type should be used instead of an imported type. In general, narrow declarations are safer than broad ones when names overlap.
A decision process
Use this sequence when reviewing a file:
- Count how many imported types come from the same module.
- Check whether the module has common, recognizable APIs.
- Search for simple-name collisions with other modules and local declarations.
- Decide whether a future reader benefits more from brevity or from visible package names.
- Use explicit imports for the few types that are central, sensitive, or ambiguous.
Exercise
Exercise
A class uses List, Map, and Collectors from java.base, and Date from java.sql. Should it use only import module java.base; and import module java.sql;?
Show answer
It can, but Date becomes ambiguous if java.base also exposes java.util.Date. Use explicit imports or a fully qualified name for the intended Date type. A module import is useful for the collection and stream types, but it is not required for every type in the file.