Sub topic 8
Using Module Imports Inside a Named Module
This chapter assumes that you can write and compile basic Java classes, use package and wildcard imports, and recognize module-info.java. A named module is a module with an explicit name in a module-info.java file.
A named module controls which other modules it can read. The requires directive creates that relationship. A module import only controls which exported types are available by their simple names in one source file. It does not replace requires.
A named module must read the imported module
Suppose an application module wants to use types from java.sql. Its module-info.java must declare:
module com.example.app {
requires java.sql;
}The requires java.sql; directive makes the java.sql module readable by com.example.app. The module system can then resolve its exported packages during compilation and execution.
A source file in that module can use a module import:
import module java.sql;
public class Main {
public static void main(String[] args) {
int integerType = Types.INTEGER;
System.out.println("JDBC INTEGER code: " + integerType);
}
}The declaration import module java.sql; makes public top-level types from packages exported by java.sql available for simple-name lookup. In this example, Types can be written instead of java.sql.Types.
The source file might be stored at:
src/com.example.app/module-info.java
src/com.example.app/com/example/app/Main.javaThe module declaration and the source file work together:
An import does not create readability
These two declarations have different jobs:
requires java.sql;changes the module graph.import module java.sql;changes name lookup in one compilation unit.
If the requires declaration is missing, the import cannot grant access to java.sql. The compiler will report that the module is not readable or that the imported module cannot be found in the module graph.
The reverse is also possible. A named module can read java.sql while a source file uses traditional imports:
import java.sql.Types;Module imports are a convenience. They do not change access control, encapsulation, or the module graph.
Readability through `requires transitive`
A module can expose a dependency to modules that read it by using requires transitive. For example:
module com.example.database.api {
requires transitive java.sql;
exports com.example.database.api;
}If another named module declares:
module com.example.client {
requires com.example.database.api;
}then com.example.client reads java.sql through the transitive dependency. A source file in com.example.client can therefore write:
import module java.sql;The transitive relationship makes java.sql readable. The module import still only makes its exported types available by simple name. It does not import types from com.example.database.api unless that module is imported separately or its packages are imported normally.
A transitive dependency should describe part of a module's public API. Do not add requires transitive merely to make imports convenient. It increases the set of modules that downstream code can read.
Why this matters in a named module
The module system checks dependencies at both compile time and run time. Compile the example with JDK 25 or later from the project directory:
javac -d out --module-source-path src -m com.example.app
java --module-path out -m com.example.app/com.example.app.MainThe -m option tells javac which named module to compile. The java command uses the module path to locate the compiled module and names the class inside that module.
If java.sql is available in the JDK but requires java.sql; is absent, adding import module java.sql; is not enough. Add the dependency to module-info.java, then compile again.
Choosing the declaration
Use a module import when a source file intentionally uses many public types from one stable module and the broader set improves readability. Keep traditional imports when the source uses only a few types, when the origin of each type should be obvious, or when several modules have overlapping simple names.
A module import is most useful after the module graph is already correct. It is a source-level name convenience, not a dependency-management feature.
Exercise
A named module declares requires java.sql;. Can a source file in that module use import module java.sql; without adding any other module declaration?
Show answer
Yes. The requires directive makes java.sql readable, and the source file can then import its exported public top-level types with the module import declaration.