TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
opens directive is a module declaration statement within the Java Platform Module System (JPMS) that grants runtime-only, deep reflective access to all types and members (including private, protected, and package-private) within a specified package. Unlike the exports directive, opens does not make the package’s public types accessible at compile time, strictly enforcing encapsulation during compilation while permitting dynamic introspection and modification at runtime.
Syntax
The directive is declared inside amodule-info.java file and can be applied universally to all reading modules (unqualified) or restricted to specific modules (qualified).
Technical Mechanics
- Deep Reflection: By default, JPMS strongly encapsulates modules, meaning the
java.lang.reflectAPI andjava.lang.invoke.MethodHandlescannot bypass access modifiers (e.g., callingsetAccessible(true)on a private field throws anInaccessibleObjectException). Theopensdirective explicitly disables this restriction for the target package. - Compile-Time Invisibility: A package that is opened but not exported remains completely invisible to the Java compiler (
javac). Any attempt by another module toimportor statically reference classes from an opened package will result in a compilation error. - Package Granularity: The directive applies strictly to the exact package specified. It is not hierarchical; opening
com.exampledoes not implicitly opencom.example.util. - Additive Nature: A package can be both exported and opened simultaneously if a module requires both compile-time public access and runtime deep reflection.
Open Modules
If an entire module requires deep reflection across all of its packages, theopen modifier can be applied to the module declaration itself. This implicitly applies the opens directive to every package within the module, rendering individual opens directives unnecessary.
opens vs. exports
Understanding the mechanical difference between these two directives is critical for JPMS access control:
| Directive | Compile-Time Access | Runtime Access | Deep Reflection (setAccessible) |
|---|---|---|---|
exports | Yes (Public types only) | Yes (Public types only) | No |
opens | No | Yes (All types) | Yes (Private/Protected members) |
Master Java with Deep Grasping Methodology!Learn More





