Sub topic 9
Implied Readability with `requires transitive`
Assume you know Java packages, access modifiers, interfaces, javac, java, and the basic purpose of module-info.java. This chapter explains how a module can pass one of its dependencies to modules that depend on it.
The problem `requires transitive` solves
Suppose a library exposes java.sql.Date in a public method:
public Date createdOn() { ... }The library must read java.sql. A normal requires java.sql lets the library compile and run, but a client module that reads the library does not automatically read java.sql.
The client must declare every module it uses directly. That is usually the clearest rule. However, some dependencies are part of a library's public API. In that case, forcing every client to repeat the dependency is unnecessary. requires transitive makes the dependency readable to clients too.
A library with an implied dependency
// src/com.example.records/module-info.java
module com.example.records {
requires transitive java.sql;
exports com.example.records.api;
}
// src/com.example.records/com/example/records/api/Record.java
package com.example.records.api;
import java.sql.Date;
public class Record {
private final Date createdOn;
public Record(Date createdOn) {
this.createdOn = createdOn;
}
public Date createdOn() {
return createdOn;
}
}
// src/com.example.app/module-info.java
module com.example.app {
requires com.example.records;
}
// src/com.example.app/com/example/app/Main.java
package com.example.app;
import com.example.records.api.Record;
import java.sql.Date;
public class Main {
public static void main(String[] args) {
Record record = new Record(Date.valueOf("2026-01-01"));
System.out.println(record.createdOn());
}
}The application declares only requires com.example.records. Because that module declares requires transitive java.sql, the application can read java.sql and use Date.
Compile and run it from a source tree containing both module directories:
javac -d out --module-source-path src -m com.example.app
java --module-path out -m com.example.app/com.example.app.MainReadability is not exporting
requires transitive creates implied readability. It does not re-export the dependency's packages through the library. The application still accesses Date because java.sql exports its own package.
| Declaration | Effect on a dependent module |
|---|---|
| `requires library` | Reads `library` only |
| `requires transitive library` | Reads `library` and the library's transitive dependencies |
Use the transitive form only when the dependency is part of the module's supported public API. Otherwise, clients become coupled to an implementation choice. Changing that dependency can then require changes in every client.
Exercise
A library uses an XML parser internally, but no parser type appears in its public methods. Should it declare requires transitive for the parser module?
Show answer
No. It should use a normal requires because the parser is an implementation detail, not part of the library's public API.