Module Import Declarations (Java 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
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());
}
}[Advitha, Navika, Srihitha]
{Advitha=7, Navika=6, Srihitha=8}
THURSDAY
srcHow it works
import module java.base;makes every class fromjava.base's exported packages usable by its simple name.List,ArrayList,Collections,MapandTreeMapcome fromjava.util;LocalDatefromjava.time;Pathfromjava.nio.file. No separate imports were needed.- 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:
java --list-modules
java --describe-module java.baseModule 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:
import module java.base;
import module java.sql;
public class ModClash {
Date when;
}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 errorThe fix is the same as for wildcard imports: add a single-type import, which always takes priority:
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());
}
}java.util.Date| Import kind | Priority 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
- Easy: Rewrite a program that uses
Scanner,ArrayListandLocalDatewith a singleimport module java.base;. - Harder: Write a class that imports
java.baseandjava.sqlas modules and uses bothjava.util.Dateandjava.sql.Date. Resolve the clash with one single-type import and one fully qualified name.
Key Takeaways
Was this tutorial helpful?