Module Import Declarations (Java 25)

Naveen AnamaneniUpdated · 4 min read
mediumJava 25javapackagesjava-25

A small program that reads a file, sorts some names and prints a date needs imports from java.util, java.nio.file and java.time, which is three or more lines before any real code. Java 25 made a shortcut standard: import module java.base; imports, on demand, every public class from every package that the java.base module exports. That's java.util, java.io, java.nio.file, java.time, java.math, java.util.function, java.util.stream and dozens more, in one line.

One Line Instead of Many

ModImport.java▶ Try it
import module java.base;
 
public class ModImport {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(
            List.of("Srihitha", "Navika", "Advitha"));
        Collections.sort(names);
        Map<String, Integer> lengths = new TreeMap<>();
        for (String n : names) {
            lengths.put(n, n.length());
        }
        System.out.println(names);
        System.out.println(lengths);
        System.out.println(LocalDate.of(2026, 11, 12).getDayOfWeek());
        System.out.println(Path.of("bank", "src").getFileName());
    }
}
Output
[Advitha, Navika, Srihitha]
{Advitha=7, Navika=6, Srihitha=8}
THURSDAY
src

How it works

  1. import module java.base; makes every class from java.base's exported packages usable by its simple name.
  2. List, ArrayList, Collections, Map and TreeMap come from java.util; LocalDate from java.time; Path from java.nio.file. No separate imports were needed.
  3. Like a wildcard import, it's resolved at compile time and changes nothing at runtime.

What Is a Module?

Since Java 9, the JDK is split into modules: named groups of packages with a declared list of what they export. java.base holds the core packages every program can use; other modules include java.sql (JDBC), java.net.http (the HTTP client) and java.desktop (Swing and AWT). You can list them all:

Terminal
java --list-modules
java --describe-module java.base

Module import declarations work with any module your code can read, including JDK modules and your own modules (a later lesson covers writing them).

Resolving Name Clashes

Importing whole modules makes clashes more likely. Both java.base (through java.util) and java.sql export a class named Date:

ModClash.java
import module java.base;
import module java.sql;
 
public class ModClash {
    Date when;
}
Compiler output
ModClash.java:5: error: reference to Date is ambiguous
    Date when;
    ^
  both class java.sql.Date in java.sql and class java.util.Date in java.util match
1 error

The fix is the same as for wildcard imports: add a single-type import, which always takes priority:

ModFix.java▶ Try it
import module java.base;
import module java.sql;
import java.util.Date;
 
public class ModFix {
    public static void main(String[] args) {
        Date when = new Date(0);
        System.out.println(when.getClass().getName());
    }
}
Output
java.util.Date
Import kindPriority when names clash
Single-type: import java.util.Date;Highest: always wins
On demand: import java.util.*;Lower; must not clash with another on-demand import you use
Module: import module java.base;Same as on-demand

Common Mistakes

Expecting it to import non-exported packages. Only exported packages are included; internal JDK packages stay hidden.

Assuming it adds the module to the build. It only shortens names; the module must already be readable (JDK modules always are on the classpath).

Using it on an old JDK. Module imports were a preview in Java 23 and 24 and became standard in Java 25.

Practice

  1. Easy: Rewrite a program that uses Scanner, ArrayList and LocalDate with a single import module java.base;.
  2. Harder: Write a class that imports java.base and java.sql as modules and uses both java.util.Date and java.sql.Date. Resolve the clash with one single-type import and one fully qualified name.

Quick check: Module imports

4 questions
1What does import module java.base; import?
2Since which version is it standard?
3Two module imports both provide Date. How do you pick one?
4Which source files import java.base automatically?

Key Takeaways

Was this tutorial helpful?

Lesson 150 of 482