Sub topic 8
Using module imports inside a named module
This chapter assumes you can compile and run a basic Java program and know the difference between import java.util.List; and import java.util.*;. A rough idea of module-info.java helps. The earlier chapters covered what import module M; imports and how name clashes are resolved. This one covers what changes when the source file belongs to a named module.
Named and unnamed modules
A source file is in a named module when a module-info.java file describes the code around it. Otherwise the file is in the unnamed module. That is the case for a single javac Hello.java run or for code on the classpath.
The two cases differ in who decides which modules the code can see.
- In the unnamed module, you write no
requiresdirectives. The compiler builds the module graph from default root modules and from any--add-modulesoptions. The unnamed module reads every module in that graph, soimport module java.sql;works with no extra setup. - In a named module,
module-info.javadecides. The module reads onlyjava.base, the modules it names inrequires, and the modules those modules pass on withrequires transitive.
A module import declaration follows the same rule as the rest of the module system. It can name a module only if the enclosing module reads it. The import adds no dependency of its own. You still write the requires directive, and the import only saves you from listing packages.
A working example
The example has two files. The module descriptor requires java.sql:
module com.example.app {
requires java.sql;
}The class imports two modules:
package com.example.app;
import module java.base;
import module java.sql;
public class Main {
private static final Logger LOG = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
Timestamp ts = Timestamp.valueOf("2026-01-15 10:30:00");
List<Timestamp> stamps = List.of(ts);
LOG.log(Level.INFO, "Loaded {0} timestamp(s)", stamps.size());
System.out.println(stamps.get(0));
}
}Three groups of simple names appear in this class.
Listcomes fromjava.base.Timestampcomes fromjava.sql.LoggerandLevelcome fromjava.logging. The file never imports that module andmodule-info.javanever requires it.
The third group works because of requires transitive.
Why Logger and Level resolve
The java.sql module declares requires transitive on three other modules. Any module that requires java.sql therefore also reads java.logging, java.xml and java.transaction.xa. A module import follows that readability closure: import module java.sql; imports the packages exported by java.sql and by every module it re-exports this way.
So a module that never mentions java.logging can still write import module java.logging;. Doing that is usually better style than relying on the transitive closure, because the file then says which API it depends on.
Compiling and running the module
Place the files under a directory named after the module, then compile and run them on JDK 25 or later:
# layout:
# src/com.example.app/module-info.java
# src/com.example.app/com/example/app/Main.java
javac --release 25 -d out/com.example.app \
src/com.example.app/module-info.java \
src/com.example.app/com/example/app/Main.java
java --module-path out -m com.example.app/com.example.app.MainOn JDK 23 and 24 the feature is a preview. Add --enable-preview and a --release value that matches the JDK you run:
javac --release 23 --enable-preview -d out/com.example.app \
src/com.example.app/module-info.java \
src/com.example.app/com/example/app/Main.java
java --enable-preview --module-path out -m com.example.app/com.example.app.MainThe program prints the logger line to standard error and 2026-01-15 10:30:00.0 to standard output.
When the module is not read
Suppose the descriptor requires only java.logging, and the class still asks for java.sql:
module com.example.app {
requires java.logging;
}package com.example.app;
import module java.sql;
public class Main {
public static void main(String[] args) {
System.out.println("never compiled");
}
}This does not compile. The module com.example.app does not read java.sql, so the import declaration is a compile-time error. The error is on the import line itself, even if no type from java.sql is used. The fix is the same as for any missing dependency: add requires java.sql; to module-info.java.
Reading and exporting are two separate checks, and a module import needs both:
| Check | Question | Failure |
|---|---|---|
| Readability | Does the enclosing module read the imported module? | Compile-time error on the `import module` line |
| Export | Does the imported module export the package of this type to the enclosing module? | The type is simply not imported, so using its name gives a "cannot find symbol" error |
A package that a module does not export, or exports only to other named modules with a qualified exports ... to, adds nothing to your imports. This matches the rule for ordinary imports, where a non-exported package is not accessible either.
java.base needs no requires
Every named module implicitly reads java.base. This means import module java.base; always compiles in a named module, and you never write requires java.base; (although the compiler accepts it). Nothing else is implicit. If you want java.sql, java.net.http or java.desktop, you must require it, directly or through a transitive chain.
Ambiguity is more likely in named modules
A named module often requires several large modules, and each one widens the set of simple names that a single import brings in. The usual rules from the earlier chapter still apply. A clash is an error only when you use the name, and a single-type import or a type declared in the file wins over module imports.
Here the descriptor requires java.sql and the class imports both modules, so Date is ambiguous:
package com.example.app;
import module java.base;
import module java.sql;
import java.util.Date;
public class Report {
public static void main(String[] args) {
Date now = new Date(); // java.util.Date, chosen by the single-type import
java.sql.Date day = new java.sql.Date(now.getTime());
System.out.println(day);
}
}Remove the import java.util.Date; line and the first use of Date stops compiling. Keep the single-type import for the name you use most, and write the other one fully qualified.
A less obvious clash comes from the transitive modules. java.sql pulls in java.xml, which exports packages such as org.w3c.dom. A module import of java.sql therefore brings in names like Node, Element and Text. They cause no trouble until you also import a module that defines a type with the same name.
Choosing the import form in a named module
Some practical rules follow from the readability requirement.
- Import a module that you list in
requires. This keeps the descriptor and the source files telling the same story. - Prefer naming a module directly over relying on
requires transitivefrom another module. Ifjava.sqlever stopped re-exportingjava.logging, your file would break. - Do not add a
requiresonly to enable an import. Add dependencies for the code that needs them. - In a library module, be conservative. The import is invisible to consumers and affects only the file it is in, but large imports hide which packages the file actually depends on.
- Keep the imports on the top of each file. Module import declarations go with the other imports, before the first type declaration.
What stays the same
- Module imports are resolved at compile time. The compiled
.classfile refers to types by their full binary names, so a file that usesimport modulehas the same bytecode as one with explicit imports. - Only public top-level classes and interfaces from exported packages are made available. Nested types still need a qualified name or their own import.
- A module import is an on-demand import. It has lower precedence than a single-type import and than a type declared in the same compilation unit or package.
Summary of the named-module rules
| Situation | Result |
|---|---|
| `import module java.base;` in any named module | Compiles. `java.base` is always read |
| `import module java.sql;` with `requires java.sql;` | Compiles |
| `import module java.logging;` with only `requires java.sql;` | Compiles, through `requires transitive` in `java.sql` |
| `import module java.sql;` with no path to `java.sql` | Compile-time error on the import |
| Type in a package that the module does not export to you | Not imported |
| Two imported modules export the same simple name | Error only where the name is used, unless a single-type import settles it |
Exercise
A module has requires java.net.http; in its descriptor, and java.net.http itself requires only java.base. A file in the module starts with import module java.net.http; and also uses Logger.getLogger("x") with no other imports. Will it compile, and why?
Show answer
It will not compile. The import module java.net.http; line is fine because the module requires java.net.http. But Logger lives in java.util.logging, in the java.logging module. The module does not require java.logging, and java.net.http does not re-export it with requires transitive, so java.logging is not read and Logger is not imported. Fix it by adding requires java.logging; to module-info.java and import module java.logging; to the file.
Exercise
A named module has requires java.sql;. A file starts with import module java.base; and import module java.sql;, and contains Date d = new Date();. Give two different ways to make it compile, and say which Date each one gives you.
Show answer
Add the single-type import import java.util.Date;, which gives java.util.Date. Or add import java.sql.Date;, which gives java.sql.Date. Writing the type fully qualified (for example java.util.Date d = new java.util.Date();) also works. A single-type import takes precedence over both module imports, so the ambiguity no longer applies.