Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Java 25 finalizes module import declarations. What does import module java.base; do, does it require your code to be a module, and how does it interact with normal single-type and on-demand imports?

import module java.base; imports, on demand, every public top-level type in every package that java.base exports, and the same for modules it requires transitively, so List, Map, Path and Stream all become available at once. The importing code does not need to be a module; it works in ordinary class path code. Single-type imports such as import java.util.List; take precedence over it and resolve ambiguities, and compact source files with instance main methods, also final in Java 25, import java.base automatically.

Why this question is asked

Java 25 made module import declarations final through JEP 511, and they change something every Java file starts with: its imports. Interviewers ask about them to check that we know what they really import, how they interact with the import rules we already know, and when they are a good idea. In this answer, we will recap how imports work, see exactly what a module import brings in, check the rule about modules, work through precedence and shadowing, and look at the compact source files that use them automatically.

1A Quick Recap of Java Imports

An import only affects how the compiler resolves simple names in one source file; it does not load anything at run time. Java has always had two kinds of type import. A single-type import, import java.util.List;, makes one name available. An on-demand import, import java.util.*;, makes every public type of one package available when it is needed. On top of those, java.lang is imported implicitly in every file.

A module groups packages and declares which of them it exports, meaning which are its public API. java.base, the module every Java program uses, exports java.lang, java.util, java.io, java.nio.file, java.time, java.util.function, java.util.stream and many more.

2What import module Brings In

A module import is an on-demand import of many packages at once: every package the named module exports, plus every package exported by modules it requires transitively, which means modules it passes on to its own users.

Java
import module java.base;
public class Report {
public static void main(String[] args) throws Exception {
List<String> lines = Files.readAllLines(Path.of("data.txt")); // java.util, java.nio.file
Map<String, Long> counts = lines.stream() // java.util.stream
.collect(Collectors.groupingBy(l -> l, Collectors.counting()));
System.out.println(counts.size() + " at " + LocalDate.now()); // java.time
}
}
// Compiles with no other imports; four packages resolved through one line

Notice that one line replaced imports from java.util, java.util.stream, java.nio.file and java.time. The transitive part matters for modules like java.sql: it requires java.logging, java.transaction.xa and java.xml transitively, so import module java.sql; also brings in types from those modules' exported packages. Only public top-level types are imported; nested types still need their outer class name, as with Map.Entry.

3Does the Code Need to Be a Module?

No. This is a common misunderstanding, because the declaration uses the word module. A class on the class path, in the unnamed module, can write import module java.base; and compile normally. The only requirement is that the named module is available to the compilation; for JDK modules such as java.base, java.sql and java.desktop, that is the case by default. Inside a named module, the importing module must read the module it imports, usually through a requires clause; otherwise the compiler reports an error. Check your build tool's support for Java 25 release level, since the feature needs --release 25 or later.

4Precedence and Shadowing

When several imports could supply the same simple name, Java uses a ranking. A single-type import wins over everything. A normal package on-demand import, such as import java.util.*;, wins over a module import. Module imports are the weakest. And if two imports of the same rank both supply a name, using that name is an ambiguity error at compile time.

Java
// Before: two module imports both supply Date
import module java.base; // exports java.util with java.util.Date
import module java.sql; // exports java.sql with java.sql.Date
...
Date d = new Date(); // error: reference to Date is ambiguous
// After: the same file with one single-type import added
import module java.base;
import module java.sql;
import java.sql.Date; // shadows both on-demand imports
...
Date d = new Date(0L); // java.sql.Date: the single-type import wins
Which import wins for a simple name
RankKind of importExample
1, strongestSingle-type importimport java.util.List;
2Package on-demand importimport java.util.*;
3, weakestModule importimport module java.base;
Same rank, two matchesAmbiguity error when the name is usedjava.util.Date and java.sql.Date

Notice that the error only happens when the ambiguous name is used. Code that never mentions Date compiles fine with both module imports. That is why the explicit single-type import is the standard fix: it states which Date we mean and shadows both on-demand imports.

5Compact Source Files and Where Module Imports Fit

Java 25 also finalised compact source files and instance main methods (JEP 512). A file can now contain just methods and fields without a class declaration, and main can be an instance method without parameters. Such a file automatically imports java.base, so beginners can use List or Files without writing any import at all.

Java
// Hello.java, a compact source file; run with: java Hello.java
void main() {
var names = List.of("Ada", "Linus"); // java.util.List, imported automatically
IO.println(names.size()); // 2
}

This shows the main purpose of module imports: less import boilerplate in scripts, teaching, prototypes and small tools. In large codebases, IDEs already manage explicit imports for us, and explicit imports document exactly which types a file depends on, so many teams keep them there.

6Follow-up questions

Which module would you import to get java.sql types?

import module java.sql;. It exports java.sql and javax.sql, and because it requires java.logging, java.transaction.xa and java.xml transitively, their exported packages come along too. Combined with import module java.base;, watch out for Date, which exists in both java.util and java.sql; a single-type import of the one we mean resolves it.

Does import module make the program load more classes or run slower?

No. Imports are a compile-time naming convenience. The compiled class file refers to every type by its fully qualified name, so the bytecode is the same as with explicit imports, and classes are loaded only when used.

Can I import my own module this way?

Yes, if it is a named module that the code can read, which includes an automatic module on the module path. The declaration imports the packages that module exports to the importing code. A plain jar on the class path has no module name, so it cannot be imported this way; we keep normal imports for it.

7Conclusion

In this answer, we saw that import module M imports on demand every public top-level type from the packages M exports, including those of modules it requires transitively, and that it works in ordinary class path code. Module imports are the weakest kind of import: package on-demand imports and single-type imports shadow them, and a single-type import is the usual fix for ambiguities such as Date. Compact source files import java.base automatically, which shows the feature's purpose: less boilerplate in scripts, teaching and prototypes, while large codebases often keep IDE-managed explicit imports.

What to say in the interviewAbout 80 seconds aloud

Open with

import module java.base; is an on-demand import of everything that java.base exports. So one line gives us every public top-level type in java.util, java.io, java.nio.file, java.time, java.util.stream and so on, and if a module requires other modules transitively, their exported packages come along too, for example java.sql brings in java.xml and java.logging.

Then, if they want more

It became final in Java 25 with JEP 511. The importing code does not have to be in a module; a normal class on the class path can use it. Inside a named module, the module just has to read the imported module.

For precedence, module imports are the weakest kind: a single-type import like import java.util.List; wins over everything, and a package on-demand import wins over a module import. If two module imports both provide a name, say Date from java.util and java.sql, using Date is a compile error, and adding a single-type import for the one I want fixes it.

Imports are purely compile time, so there is no runtime cost. Java 25 also finalised compact source files with instance main methods, and those import java.base automatically, which shows what the feature is for: scripts, teaching and prototypes. In a large production codebase I would usually keep explicit imports managed by the IDE.

About this answer

Last updated
12 November 2026
Versions it names
Java 25
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Java 25 finalizes module import declarations. What does import module java.base; do...": https://scrivio-interview-library.netlify.app/java/modules/4ec16b3581/