Sub topic 6
Modules Reached Through `requires transitive`
This chapter assumes that you can write and compile basic Java classes, use single-type and wildcard imports, and understand packages. It also assumes that you have seen module-info.java and the requires directive.
A Java module can depend on another module. The dependency is declared with requires:
module com.example.reports {
requires java.sql;
}The com.example.reports module can use types exported by java.sql. The java.sql module itself depends on other modules. Some of those dependencies are declared with requires transitive.
What `requires transitive` means
Suppose module A declares:
module A {
requires transitive B;
}Module A reads module B. A different module that requires A also reads B:
module C {
requires A;
}The transitive dependency creates this readability path:
C → A → BWithout transitive, C would read A, but not automatically read B.
| Declaration | Direct reader | Consumers also read the dependency |
|---|---|---|
| `requires B` | The declaring module | No |
| `requires transitive B` | The declaring module | Yes |
A transitive dependency is appropriate when the dependency appears in the public API of the module. For example, if public methods in module A accept or return types from module B, users of A may need to read B too.
How module imports use readability
An import module declaration has this form:
import module java.sql;It makes public top-level types in packages exported by java.sql available by simple name. It also reaches modules that java.sql reads. This includes modules exposed through its module relationships, including transitive dependencies.
The import does not change the module graph. It only changes which type names the source file can use without package qualifiers. The enclosing module must still read the imported module.
For example, java.sql has a transitive relationship with java.logging. A source file importing java.sql can therefore refer to both DriverManager from java.sql and Logger from java.logging by simple name:
import module java.sql;
public class TransitiveImportDemo {
public static void main(String[] args) {
Logger logger = Logger.getLogger("demo");
logger.info("Checking available JDBC drivers");
boolean driverAvailable = DriverManager.getDrivers().hasMoreElements();
System.out.println("Driver available: " + driverAvailable);
}
}On a JDK where module imports are final, save this as TransitiveImportDemo.java and compile it with:
javac --release 25 TransitiveImportDemo.java
java TransitiveImportDemoThe source does not contain import java.logging.Logger;. The module import makes the type available because the imported module's readability graph reaches java.logging.
A module import can expose many names. That convenience can also introduce collisions.
Named modules and transitive readability
Consider an application module that imports java.sql:
module com.example.app {
requires java.sql;
}The requires java.sql declaration is still necessary. Even though java.sql can read other modules, the source module must first read java.sql itself.
A module imported by the source can be reached in two ways:
- The source module directly declares
requiresfor it. - The source module requires another module whose
requiresdeclaration is transitive.
For example:
module com.example.database {
requires transitive java.sql;
}A consumer can write:
module com.example.app {
requires com.example.database;
}The application reads java.sql through com.example.database. In source code inside com.example.app, import module java.sql; is permitted because the application reads that module.
The important distinction is between readability and importing:
requiresestablishes a module-graph relationship.requires transitivepasses that relationship to downstream modules.import modulemakes exported types from the selected module and its reachable modules available by simple name.
Do not confuse transitive modules with transitive imports
A transitive requirement does not automatically place every type into every source file's namespace. A source file still needs an import declaration, or it must use a fully qualified name.
For example, this module declaration can make java.sql readable:
module com.example.app {
requires transitive com.example.database;
}But a class does not automatically gain the simple name DriverManager. It must use either:
import module java.sql;or:
import java.sql.DriverManager;The module graph answers “may this module access the other module?” Import declarations answer “which type names may this source file use simply?”
Choosing the dependency style
Use requires transitive when a dependency is part of your module's public contract and consumers need to access its types. Use ordinary requires for an implementation detail.
Use import module when the module's exported types form a coherent working set and the shorter source is easier to read. Prefer explicit imports when a module exports many unrelated types or when simple-name collisions are likely.
Exercise
Module A declares requires transitive B, and module C declares requires A. Does C automatically import every type from B?
Show answer
No. C reads B through the transitive requirement, but a source file in C still needs an import declaration or a fully qualified name to use a type from B by simple name.