Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The 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 a module-info.java file and can be applied universally to all reading modules (unqualified) or restricted to specific modules (qualified).
module com.example.core {
    // Unqualified opens: Grants deep reflection access to all modules at runtime
    opens com.example.core.internal;

    // Qualified opens: Grants deep reflection access exclusively to specified modules
    opens com.example.core.security to com.example.auditor, com.example.monitor;
}

Technical Mechanics

  • Deep Reflection: By default, JPMS strongly encapsulates modules, meaning the java.lang.reflect API and java.lang.invoke.MethodHandles cannot bypass access modifiers (e.g., calling setAccessible(true) on a private field throws an InaccessibleObjectException). The opens directive 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 to import or 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.example does not implicitly open com.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, the open 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.
open module com.example.legacy {
    // All packages within this module are implicitly opened for deep reflection.
    // You can still use 'exports' to grant compile-time access.
    exports com.example.legacy.api;
}

opens vs. exports

Understanding the mechanical difference between these two directives is critical for JPMS access control:
DirectiveCompile-Time AccessRuntime AccessDeep Reflection (setAccessible)
exportsYes (Public types only)Yes (Public types only)No
opensNoYes (All types)Yes (Private/Protected members)
Master Java with Deep Grasping Methodology!Learn More