Sub topic 2
Recap: Modules, Exports and Readability
This chapter assumes that you can write and compile basic Java classes, use package names, and distinguish single-type imports from wildcard imports. You should also have seen module-info.java and the requires directive.
Java packages group related types. A Java module groups packages and declares which other modules it needs. The module declaration is normally stored in a file named module-info.java.
A module declaration can name its module, the modules it reads, and the packages it makes available:
module com.example.report {
requires java.logging;
exports com.example.report.api;
}This declaration says that com.example.report reads java.logging. It also exports its com.example.report.api package. Other modules that can read com.example.report may use the public types in that exported package.
Exports control access
An exports directive exposes a package outside its module:
module com.example.report {
exports com.example.report.api;
}This does not export every package in the module. For example, these packages have different visibility:
| Package | Exported? | Usable by another module? |
|---|---|---|
| `com.example.report.api` | Yes | Yes, for public types |
| `com.example.report.internal` | No | No |
Only public top-level types in an exported package are available to another module through normal source-level access. A public class in a non-exported package remains encapsulated inside its module.
A module can also export a package only to selected modules:
module com.example.report {
exports com.example.report.api to com.example.client;
}This is called a qualified export. The package is available to com.example.client, but not generally to every other module.
Readability controls which modules can be used
A module must read another module before its code can use that module's accessible types. The requires directive creates this relationship:
module com.example.client {
requires com.example.report;
}Readability and accessibility work together:
- The client module must read the provider module.
- The provider module must export the needed package.
- The type and member being used must be accessible under Java's normal access rules.
An exported package without a readable module is still unusable by a named module. Likewise, a readable module does not expose its non-exported packages.
`requires transitive` passes readability onward
A normal requires relationship affects only the module that declares it. Consider three modules:
module com.example.client {
requires com.example.report;
}module com.example.report {
requires com.example.format;
exports com.example.report.api;
}The client reads com.example.report, but it does not automatically read com.example.format. If the client's source code directly uses an exported type from com.example.format, the client must declare its own requires com.example.format.
A module can deliberately pass readability to its consumers:
module com.example.report {
requires transitive com.example.format;
exports com.example.report.api;
}Now a module that requires com.example.report also reads com.example.format. This is useful when the report module's public API exposes types from the format module. It also creates a stronger dependency, so it should be used only when that relationship is part of the public API.
Named and unnamed modules
A named module has a name and a module-info.java descriptor. It participates explicitly in the module graph.
Code on the class path belongs to the unnamed module. The unnamed module has no module-info.java and does not declare requires or exports. It can read named modules that are observable in the runtime environment, while a named module cannot rely on reading class-path code through ordinary module readability.
This distinction matters when migrating an application gradually. A class-path application can often use modular libraries without becoming a named module itself. Once the application becomes a named module, its dependencies should be declared explicitly.
The next chapter introduces import module, which uses a module's exported types while leaving this readability and access model unchanged.
Exercise
Module A requires module B. Module B does not export the package containing class C. Can A use public class C?
Show answer
No. A can read B, but B does not export C's package. Both readability and an appropriate export are required.