The Readability Graph
You'll learn how requires directives create a directed graph of module relationships that the JVM uses to resolve dependencies at startup.
What Is the Readability Graph?
When you declare requires directives in module-info.java, you're not just listing dependencies—you're building a directed graph where each module is a node and each requires creates an edge. This is the readability graph, and the JVM constructs it at startup to determine which modules can "read" (access) which other modules.
Think of it like a network of trust relationships: if Module A requires Module B, then A can read B's exported packages. The graph must be acyclic (no circular dependencies) and complete (all dependencies resolved) for the application to start.
How the JVM Resolves the Graph
At startup, the JVM:
- Starts with your root module (the one containing
main) - Recursively resolves all
requiresdirectives, adding modules to the graph - Follows
requires transitiveedges to include indirect dependencies - Validates that all required modules exist and are accessible
- Checks that no cycles exist
- Enforces strong encapsulation based on
exportsdeclarations
If any module is missing or a cycle is detected, startup fails with a ResolutionException.
Why This Matters
The readability graph isn't just theoretical—it's the foundation of module security and encapsulation. A module can only access types from modules it reads (directly or transitively). The graph is computed once at startup, making dependency resolution predictable and fast.
Unlike classpath's flat structure where everything could see everything, the readability graph enforces explicit, verifiable relationships.
Key Takeaway:
The readability graph is a directed, acyclic graph built from requires directives that the JVM constructs at startup to enforce module encapsulation and resolve dependencies.