00:00

Module Import Declarations in Java 25

Last updated: · Standard (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

Imports.java
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.sql brings in java.xml and java.logging.

3. Practical Examples

Example 1: One import for the core library

ModuleImport.java
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());
    }
}
Output
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.

AmbiguousDate.java
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);
    }
}
Compiler output
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

ResolvedDate.java
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
    }
}
Output
java.sql.Date = 2025-09-16
JDBC type: DATE

4. How a Simple Name Is Resolved

Order of import resolution The compiler first looks at single-type imports, then type-import-on-demand of packages, then module imports. Ambiguity at the same level is an error. single-type import import java.sql.Date package on demand import java.util.* module import import module java.base same name twice? ambiguity error
Figure 1: More specific imports shadow less specific ones.

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.base automatically.

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

7. References