Sub topic 4
What Exactly Gets Imported
Assume you already know ordinary single-type imports, wildcard imports, packages, and the basic purpose of Java modules. This chapter focuses on the names made available by an import module declaration.
A module import is a type import
A module import has this form:
import module module.name;For example:
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"));
}
}The declaration makes public top-level types from packages exported by java.base available by simple name. In this example, List, Map, HashMap, and Collectors can be used without their package names.
The declaration does not import objects, methods, fields, or packages as values. It affects how the compiler resolves type names in the source file.
The export boundary matters
A module contains packages, and a module declaration decides which packages it exports. An import module declaration considers only exported packages.
It does not make every type inside the module visible. A type must meet all of these conditions:
| Condition | Meaning |
|---|---|
| It is top-level and public | A nested type or package-private type is not imported by its simple name |
| Its package is exported by the module | Internal packages remain hidden |
| The module is readable from the current module | The source module must have access to the imported module |
A public class in an unexported package remains unavailable through a module import. This preserves the module's encapsulation boundary.
The imported names are selected at compile time. The declaration does not copy classes into your program or change the module's exports.
What is not included
A module import is broader than a package wildcard import, but it still has precise limits.
It does not import:
- Types from packages that the module does not export.
- Package-private top-level types.
- Private, protected, or package-private nested types as simple names.
- Static members such as
Collections.emptyList. - Methods or fields.
- Types from modules merely mentioned by the imported module's implementation.
For example, import module java.base; does not mean that every implementation detail used inside java.base becomes available. It exposes the public API of its exported packages, subject to normal module readability rules.
An imported type can still be used with its fully qualified name. The module import only provides an additional simple-name lookup.
Exported packages versus transitive dependencies
Suppose module app requires module library. If library requires common with requires transitive, then app can read common through library. That readability relationship can allow source code in app to import types from common.
However, import module library; imports types exported by library, not every type from modules that library reads. To import types exported by common, write a separate declaration:
import module library;
import module common;The requires transitive directive controls which modules are readable. The import module declaration controls which readable module's exported types are considered for simple-name lookup. These mechanisms work together, but they do different jobs.
Ordinary imports still apply
A module import does not replace every other import form. You can combine it with a single-type import when you need a particular name or a static member:
import module java.base;
import java.util.Collections;
public class ExplicitType {
public static void main(String[] args) {
List<String> values = Collections.emptyList();
System.out.println(values);
}
}Here, List comes from the module import, while Collections is named explicitly. This can make important dependencies easier to see.
When you need a type that is not exported, or when you want to avoid a broad set of possible names, use its explicit package name or an ordinary import.
Exercise
Does import module java.base; import every class physically contained in the java.base module?
Show answer
No. It makes public top-level types from packages exported by java.base available by simple name. Non-exported packages, non-public types, members, and implementation details are excluded.