Sub topic 4
What Exactly Gets Imported
This chapter assumes you know single-type and on-demand imports, and that a package is a namespace for types. It also assumes a rough idea that a module has a module-info.java with exports and requires directives. The previous chapter introduced the syntax import module java.base;. This chapter covers which names that line brings into scope and which it leaves out.
The rule
A module import declaration works like an on-demand import (import java.util.*;) applied to every package the module exports. It makes these types available by simple name:
- Each public top-level class, interface, enum, record, and annotation interface.
- Declared in a package that the module exports to the module containing your source file.
- That includes packages exported to everyone and packages exported only to your module (a qualified export).
- It also includes types in packages exported by modules that
Mrequires withrequires transitive.
The declaration does not import nested types, non-public types, static members, or types in packages the module does not export.
What the rule means for common cases
| Type | Imported by `import module java.base;`? | Reason |
|---|---|---|
| `java.util.ArrayList` | Yes | Public top-level class in the exported package `java.util` |
| `java.util.stream.Collectors` | Yes | `java.base` exports `java.util.stream` as its own package |
| `java.nio.file.Path` | Yes | Public interface in the exported package `java.nio.file` |
| `java.lang.String` | Already available | `java.lang` is always imported implicitly |
| `Map.Entry` as `Entry` | No | Nested type. Write `Map.Entry`, or add `import java.util.Map.Entry;` |
| `Math.max` as `max` | No | Static members need `import static` |
| Package-private class in `java.util` | No | Not public |
| Public class in `jdk.internal.misc` | No | `java.base` does not export that package to your code |
Example: several packages from one line
The next program uses types from five packages of java.base. It needs no other import.
import module java.base;
public class WhatIsImported {
public static void main(String[] args) throws IOException {
Path temp = Files.createTempFile("demo", ".txt");
Files.writeString(temp, "hello");
BigDecimal price = new BigDecimal("19.99");
LocalDate day = LocalDate.of(2025, 9, 16);
Function<String, Integer> length = String::length;
System.out.println(Files.readString(temp)
+ " " + price
+ " " + day
+ " " + length.apply("abc"));
Files.delete(temp);
}
}The names come from these packages:
IOExceptionfromjava.ioPathandFilesfromjava.nio.fileBigDecimalfromjava.mathLocalDatefromjava.timeFunctionfromjava.util.function
You would otherwise need five or more import lines for them. Run it with java WhatIsImported.java on JDK 25 or later.
Imports are per package, not per package tree
java.util.stream is not imported because it lives "under" java.util. It is imported because java.base exports it as a package of its own. The same is true of traditional on-demand imports: import java.util.*; does not import java.util.stream.Collectors. A module import is a shortcut for listing every exported package of the module. It is not a rule about package names.
This has a consequence when you move between modules. A package that sits next to an imported one in the name hierarchy gives you nothing unless its module exports it, and its module is M or something M requires transitively.
Nested types and static members
Nested types are the most common surprise. The following fragment does not compile, because Entry is not a top-level type:
import module java.base;
public class NestedTypes {
public static void main(String[] args) {
Map<String, Integer> ages = Map.of("Ada", 36);
// Entry<String, Integer> first = ages.entrySet().iterator().next(); // error: cannot find symbol Entry
Map.Entry<String, Integer> first = ages.entrySet().iterator().next();
System.out.println(first.getKey() + " " + first.getValue());
}
}Map is imported, so Map.Entry works. If you want the bare name, add a single-type import for the nested type, for example import java.util.Map.Entry;. Static imports are likewise a separate mechanism. import module java.base; does not let you write max(1, 2) for Math.max(1, 2).
Inspecting what a module exports
You can ask the runtime which packages a module exports, instead of guessing. This program prints the unqualified exports of a module. Pass a module name as the first argument, or it uses java.base.
import module java.base;
public class ListExports {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "java.base";
Module module = ModuleLayer.boot().findModule(name).orElseThrow();
List<String> packages = module.getDescriptor().exports().stream()
.filter(e -> !e.isQualified())
.map(ModuleDescriptor.Exports::source)
.sorted()
.toList();
packages.forEach(System.out::println);
System.out.println(packages.size() + " unqualified exports in " + name);
}
}Run it with java ListExports.java or java ListExports.java java.sql. For java.base, the sorted output starts with java.io and java.lang. The program itself needs ModuleLayer, Module, ModuleDescriptor, and List, and one module import supplies all of them.
You can get a similar listing from the command line with java --describe-module java.base. That output also shows the qualified exports, which are packages exported only to named modules. Those are the packages your code cannot import from.
Exports of another module, and what comes with it
The module java.sql exports java.sql and javax.sql. Its declaration also contains requires transitive for java.logging, java.xml, and java.transaction.xa. One import line therefore reaches the logging API too:
import module java.sql;
public class SqlImports {
public static void main(String[] args) {
Logger log = Logger.getLogger("demo");
boolean anyDriver = DriverManager.getDrivers().hasMoreElements();
log.info("JDBC drivers registered: " + anyDriver);
}
}DriverManager comes from java.sql. Logger comes from java.util.logging, which belongs to java.logging, and java.sql requires that module transitively. The next-but-one chapters on transitive modules cover this fully.
Notice that this file imports nothing from java.base except java.lang types such as String and System. A module does not re-export java.base just because every module reads it. If this file used List, you would need import module java.base; too, and importing both modules can then create ambiguous names. The next chapter covers that.
Errors you will meet
- Module not found, or not readable. The module named in the declaration must exist in the module graph. In a named module, your own module must read it. A later chapter covers that case.
- Cannot find symbol for a nested type. The type is not top-level. Qualify it with its outer type, or add a single-type import.
- Cannot find symbol for a type you know is public. Check its package against the module's exports. Your module may not read the module that exports it, or the package may be exported only to specific modules.
- Package does not exist. A fully qualified name can fail for the same reason: the package is not exported, so the module import will not help either.
- Reference to a simple name is ambiguous. Two imported modules export the same simple name and you used it. This is the next chapter's topic.
Summary of the model
Treat import module M; as shorthand for a long list of import package.*; lines. The list covers every package that M exports to you, plus the packages exported by modules M requires transitively. The shorthand has the same limits as on-demand imports: no nested types, no static members, and no sub-package tree.
Exercise
A file contains only import module java.base;. Which of these simple names compile as written: Collectors, Entry, Path, Unsafe (the class in the non-exported package jdk.internal.misc)? Give a reason for each.
Show answer
Collectors compiles, because java.util.stream is an exported package of java.base and Collectors is a public top-level class. Path compiles for the same reason, via the exported package java.nio.file. Entry does not compile, because it is a nested type of Map; use Map.Entry or add import java.util.Map.Entry;. Unsafe does not compile, because jdk.internal.misc is not exported to your code.
Exercise
Your file has import module java.sql; and calls Logger.getLogger("x") and new ArrayList<String>(). Which call compiles, and what must you add for the other?
Show answer
Logger.getLogger("x") compiles, because java.sql requires java.logging transitively, so the exported package java.util.logging is imported. new ArrayList<String>() does not compile, because java.sql does not require java.base transitively. Add import module java.base; or import java.util.ArrayList;.