Module Import Declarations in Java 25
A module import declaration, such as import module java.base;, imports all public types from every package a module exports. It was previewed in Java 23 and 24 and became standard in Java 25 (JEP 511).
This tutorial shows how module imports shorten import lists, what happens when two modules export a class with the same name, and how to resolve it.
1. Why Were Module Imports Added?
Even simple programs need imports from several packages: java.util, java.util.stream, java.time, java.nio.file. Beginners must learn where each class lives, and experienced developers manage long import lists. A module import brings in a whole API at once, and it works in any source file, whether or not your own code is modular.
2. Syntax and Resolution Rules
import module java.base; // all exported packages of java.base
import module java.sql; // java.sql, javax.sql, and modules it requires transitively
import java.sql.Date; // single-type import: wins over module imports
- Single-type imports (
import java.sql.Date;) take priority over on-demand and module imports. - If two module imports provide the same simple name and you use it, the compiler reports an ambiguity.
- Importing a module also imports the modules it
requires transitive;java.sqlbrings injava.xmlandjava.logging.
3. Practical Examples
Example 1: One import for the core library
import module java.base; // every package exported by java.base
public class ModuleImport {
public static void main(String[] args) throws Exception {
// java.util, java.util.stream, java.time, java.nio.file... without single imports
List<String> logs = List.of("ERROR db", "INFO start", "ERROR auth", "WARN disk");
Map<String, Long> byLevel = logs.stream()
.collect(Collectors.groupingBy(l -> l.split(" ")[0], TreeMap::new, Collectors.counting()));
System.out.println("By level : " + byLevel);
LocalDate release = LocalDate.of(2025, Month.SEPTEMBER, 16);
System.out.println("Java 25 : " + release.getDayOfWeek());
Path report = Path.of("logs", "summary.txt");
System.out.println("Path : " + report.getFileName());
}
}
By level : {ERROR=2, INFO=1, WARN=1}
Java 25 : TUESDAY
Path : summary.txt
Example 2: An ambiguous name
Both java.base and java.sql export a class named Date.
import module java.base; // exports java.util.Date
import module java.sql; // exports java.sql.Date
public class AmbiguousDate {
public static void main(String[] args) {
Date d = new Date(0); // which Date?
System.out.println(d);
}
}
AmbiguousDate.java:6: error: reference to Date is ambiguous
Date d = new Date(0); // which Date?
^
both class java.sql.Date in java.sql and class java.util.Date in java.util match
AmbiguousDate.java:6: error: reference to Date is ambiguous
Date d = new Date(0); // which Date?
^
both class java.sql.Date in java.sql and class java.util.Date in java.util match
2 errors
Example 3: Resolving it with a single-type import
import module java.base;
import module java.sql;
import java.sql.Date; // a single-type import wins over module imports
public class ResolvedDate {
public static void main(String[] args) {
Date d = Date.valueOf("2025-09-16");
System.out.println(d.getClass().getName() + " = " + d);
System.out.println("JDBC type: " + JDBCType.valueOf(Types.DATE)); // from java.sql, no import
}
}
java.sql.Date = 2025-09-16 JDBC type: DATE
4. How a Simple Name Is Resolved
5. Important Points and Best Practices
- Use module imports in scripts, examples, tests and small programs.
- In large code bases, explicit imports still make dependencies easy to see in reviews.
- Add a single-type import whenever a name becomes ambiguous.
- Compact source files (JEP 512) import
java.baseautomatically.
6. Summary
- Java 25 (JEP 511) finalised
import module M;. - It imports every exported package of a module, plus transitively required modules.
- Single-type imports take priority and resolve ambiguities.
- Module imports work in any source file, modular or not.
Related Tutorials
- Compact Source Files and Instance Main Methods
- Flexible Constructor Bodies
- Strong Encapsulation of JDK Internals (Java 17)
- Java 25 Features
7. References
Frequently Asked Questions (FAQ)
Q1. What does import module java.base do?
It imports all public top-level types from every package exported by the java.base module, such as java.util, java.io and java.time.
Q2. Do I need to use modules in my project to use module imports?
No. Module import declarations work in any source file, including code on the class path.
Q3. What happens if two modules export classes with the same name?
Using that simple name is a compile error ("reference is ambiguous"). Add a single-type import to choose one.
Q4. Which Java version made module imports standard?
Java 25, through JEP 511, after previews in Java 23 and 24.