Module Import Declarations in Java: import module
import module is an import declaration that, in a single line, imports all public classes and interfaces from all packages a module exports. Instead of a dozen lines like import java.util.*;, import java.nio.file.*;, import java.time.*; you write import module java.base;. The feature became standard in Java 25 (JEP 511) and works in any file, both in a modular project and in ordinary class-path code.
import module java.base;
public class Report {
public static void main(String[] args) {
List<String> items = new ArrayList<>(List.of("pen", "book")); // java.util
Map<String, Integer> prices = new HashMap<>(); // java.util
prices.put("pen", 2);
Path file = Path.of("report.txt"); // java.nio.file
LocalDate day = LocalDate.of(2026, 11, 28); // java.time
BigDecimal total = BigDecimal.valueOf(12.5); // java.math
Function<String, String> upper = String::toUpperCase; // java.util.function
IO.println(items.stream().map(upper).toList() + " " + prices + " " + file + " " + day + " " + total);
}
}
// [PEN, BOOK] {pen=2} report.txt 2026-11-28 12.5 This lesson continues Java Modules: module-info, requires, exports and opens and Java Services. Below: what exactly gets imported, how name clashes are resolved, how import module differs in modular and ordinary code, and how it relates to compact source files. All examples were checked on JDK 25.
1. Three kinds of imports: type, package, module
Before Java 25 there were two kinds of type imports: a single type and all types of a package (packages and imports are covered in Understanding Java Packages). import module adds a third, the widest one:
| Declaration | Imports | Example |
|---|---|---|
| Single-type import | One class or interface | import java.util.List; |
| Type-import-on-demand | All public types of one package | import java.util.*; |
| Module import | All public types of all packages the module exports | import module java.base; |
The java.base module exports more than fifty packages: java.lang, java.util, java.io, java.nio.file, java.time, java.math, java.net, java.util.concurrent, java.util.function, java.util.stream and others. All of them come into scope with one line.
2. What import module imports
import module M; imports the public top-level types of the packages that module M exports to the current code. Important details:
- Exported packages only. A module's internal packages are not imported: for example,
jdk.internal.misc.Unsafefromjava.baseis not available (cannot find symbol). This holds even when a module imports itself:import module com.shop.catalog;insidecom.shop.cataloggives no access to its own non-exported package. - Each package separately, but all at once.
java.utilandjava.util.concurrentare different packages, butjava.baseexports both, so bothListandThreadLocalRandomare available. - Top-level types only. The nested interface
Map.Entryis available asMap.Entry, but plainEntryis not (cannot find symbol). - Static members are not imported. After
import module java.base;a call tomax(1, 2)does not compile; you needMath.max()orimport static java.lang.Math.*;. - Qualified exports count. If a module exports a package only to specific modules (
exports P to M), that package is imported only in the code of moduleM.
3. import module and requires transitive
Besides the module's own packages, import module M imports the packages of all modules that M requires transitively (requires transitive, see the module-info lesson). The java.sql module requires java.logging and java.xml transitively, so one line is enough for types from three modules:
import module java.sql;
public class SqlDemo {
public static void main(String[] args) {
Connection c = null; // java.sql
Logger l = null; // java.util.logging, module java.logging
DocumentBuilderFactory f = null; // javax.xml.parsers, module java.xml
Date d = new Date(0L);
System.out.println(d.getClass().getName());
}
}
// java.sql.Date But List is not found in this file: every module reads java.base, but java.sql does not require it transitively, so import module java.sql; does not import the java.base packages:
import module java.sql;
...
List<String> l = List.of("x");
// error: cannot find symbol
// symbol: class List The exception is the aggregator module java.se: it requires java.base, java.sql, java.desktop and the other Java SE platform modules transitively. So import module java.se; imports everything at once, together with the name clashes described next.
4. Name clashes and import precedence
The wider the import, the more likely two packages contain a class with the same name. The classic example is List: the interface java.util.List from java.base and the class java.awt.List from java.desktop:
import module java.base;
import module java.desktop;
public class Amb {
public static void main(String[] args) {
List<String> names = List.of("x");
System.out.println(names);
}
}
// error: reference to List is ambiguous
// both class java.awt.List in java.awt and interface java.util.List in java.util match The error appears only when the name is used. If the same file with these two imports uses only Set.of(1) and new ArrayList<>(s), it compiles: Set and ArrayList exist only in java.util. Overlapping module imports are not an error by themselves.
A clash is resolved with a "narrower" import, because the kinds of imports have different precedence:
| Precedence | Where the name comes from | Example |
|---|---|---|
| 1 (highest) | A class declared in this file or package | class List { } next to import module java.base;: your own List is used |
| 2 | Single-type import | import java.util.List; |
| 3 | Type-import-on-demand, including the implicit java.lang.* | import java.util.*; |
| 4 (lowest) | Module import | import module java.base; |
So any of three changes fixes the example above:
import module java.base;
import module java.desktop;
import java.util.List; // option 1: single-type import
// import java.util.*; // option 2: an on-demand import also beats a module import
System.out.println(List.class.getName()); // java.util.List
// option 3: the fully qualified name in code: java.util.List<String> names = java.util.List.of("x"); Note option 2: an on-demand import beats a module import. It works "the other way" too: with import module java.base; and import java.awt.*; the simple name List means java.awt.List. The order of import lines does not matter.
The second common clash is Date: java.util.Date from java.base and java.sql.Date from java.sql. With both import module java.base; and import module java.sql;, new Date(0L) gives reference to Date is ambiguous; with an added import java.sql.Date; it compiles and creates a java.sql.Date.
5. import module in a modular project
import module does not replace requires. In a named module you can import only a module the current module reads:
module com.shop.app { } // no requires com.shop.catalog
// com/shop/app/Main.java
import module com.shop.catalog;
// error: module com.shop.app does not read: com.shop.catalog The same goes for JDK modules: import module java.sql; in a module without requires java.sql gives module com.shop.app does not read: java.sql. When requires is present, the import follows all visibility rules. The com.shop.catalog module from the shop example is declared like this:
module com.shop.catalog {
requires transitive com.shop.model; // record Product
exports com.shop.catalog.api; // Catalog
exports com.shop.catalog.admin to com.shop.app; // PriceEditor
// package com.shop.catalog.internal is not exported
}
module com.shop.app {
requires com.shop.catalog;
}
// com/shop/app/Main.java
package com.shop.app;
import module com.shop.catalog;
public class Main {
public static void main(String[] args) {
Catalog catalog = new Catalog(); // com.shop.catalog.api
Product p = catalog.find("book"); // com.shop.model, via requires transitive
PriceEditor editor = new PriceEditor(); // com.shop.catalog.admin, exported only to com.shop.app
System.out.println(p + " " + editor);
}
}
// Product[id=book, price=400] PriceEditor One import line brought in types from two modules and a package with a qualified export. The class PriceRules from the non-exported internal package is still unavailable: cannot find symbol.
6. import module in code without module-info
Ordinary class-path code lives in the unnamed module, which reads the JDK modules without any requires. So import module java.sql; or import module java.net.http; compile there right away.
With one exception: by default the unnamed module does not read the aggregator java.se:
import module java.se;
// error: unnamed module does not read: java.se
javac --add-modules java.se SE.java # this compiles In practice, code without modules needs import module java.base; plus one or two imports of specific modules such as java.sql.
7. import module and compact source files
In a compact source file (Java 25, a file without a class declaration) the line import module java.base; is implied automatically. That is why such a file works without a single import. But once you import another module, name clashes show up immediately:
// File Cs.java, a compact source file
import module java.sql;
void main() {
Date d = new Date(0L);
IO.println(d.getClass().getName());
}
// error: reference to Date is ambiguous
// both class java.sql.Date in java.sql and class java.util.Date in java.util match Here java.util.Date came from the implicit import module java.base;. The fix is the same: import java.sql.Date; next to the module import.
8. Syntax, Java versions and typical errors
import module goes where ordinary imports go: after package and before the class declaration, mixed with them in any order. The history of the feature:
| Java version | JEP | Status |
|---|---|---|
| 23 | JEP 476 | Preview |
| 24 | JEP 494 | Second preview: on-demand imports now beat module imports, java.se now requires java.base transitively |
| 25 | JEP 511 | Standard feature, no --enable-preview needed |
With --release 24 or lower the compiler reports module imports are not supported in -source 24. What compiles and what does not (JDK 25, an ordinary class on the class path):
| Import line | Result |
|---|---|
import module java.base; | Compiles |
import module java.net.http; | Compiles |
import java.util.List; + import module java.base; | Compiles, order does not matter |
import module java.util; | Error: imported module not found: java.util; that is a package name, not a module |
import module com.example.nothing; | Error: imported module not found |
import module java.*; | Error: wildcards are not supported |
import static module java.base; | Error: there is no static module import |
import module java.se; | Error on the class path: unnamed module does not read: java.se; compiles with --add-modules java.se |
9. When to use import module
A module import saves lines but hides where each class comes from. A sensible approach:
- Yes: learning examples, scripts, compact source files, prototypes and JShell, where brevity matters.
- Yes: code that uses one library module with a well-designed API heavily, for example
import module com.shop.catalog;instead of five imports of its packages. - Carefully: production code in a large project. The IDE manages imports anyway, and explicit
importlines protect you from surprise clashes when a new library version adds a class with the same name.
Frequently asked questions
Does import module slow down compilation or the program?
The program, no: imports exist only in source code, and bytecode always uses fully qualified class names. The compiler just looks up simple names in more packages, which is unnoticeable in practice. The size of .class files and the set of loaded classes do not change.
Do I need module-info.java to write import module?
No. In ordinary class-path code, import module java.base; or import module java.sql; work right away. module-info.java matters only if your code is a module itself: then you can import only modules it reads through requires.
Why is List not found after import module java.sql?
import module M imports the packages of the module itself and of the modules it requires transitively. java.sql requires java.logging and java.xml transitively, but not java.base, so java.util.List is not imported. Add import module java.base; or import java.util.List;.
How do I fix "reference to List is ambiguous"?
Two imported modules contain types with that name, for example java.util.List and java.awt.List. Add an import of the type you want (import java.util.List;) or of its package (import java.util.*;): both kinds of imports beat a module import. You can also write the fully qualified class name in the code.
Does import module import static methods?
No. Only public top-level classes and interfaces are imported. Static methods and constants still need import static, for example import static java.lang.Math.*;, and nested types are written through the outer class: Map.Entry.
Comments