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 exports directive is a statement within the Java Platform Module System (JPMS) used in a module-info.java declaration to explicitly expose a specific package to other modules. By default, all packages within a Java module are strongly encapsulated. The exports directive overrides this encapsulation, granting compile-time and runtime access to the public types residing in the specified package, as well as the public and protected members (which includes nested types) of those public types.

Unqualified Exports

An unqualified exports directive makes the package accessible to any module that reads the exporting module. This readability can be established directly via a requires declaration or transitively (implied readability) via a requires transitive dependency on an intermediary module.
module com.example.core {
    // Exposes public types in this package to any reading module
    exports com.example.core.api;
}

Qualified Exports

A qualified exports directive restricts package visibility to a specific, comma-separated list of target modules. Modules not explicitly listed in the to clause are denied access, even if they read the exporting module.
module com.example.core {
    // Exposes the package exclusively to the 'com.example.metrics' and 'com.example.audit' modules
    exports com.example.core.internal to com.example.metrics, com.example.audit;
}

Technical Characteristics

  • Package-Level Granularity: The directive operates strictly at the package level. You cannot export individual classes or interfaces.
  • Non-Transitive Sub-packages: Exporting a package does not export its sub-packages. For example, exports com.example.core; does not expose com.example.core.util. Each package must be exported via its own distinct directive.
  • Access Modifier Enforcement: The directive only crosses the module boundary. Standard Java access modifiers still apply. Package-private (default) types, as well as private or package-private members within an exported package, remain inaccessible to external modules.
  • Reflection Limitations: While exports allows standard reflective access to public types, it does not permit deep reflection (e.g., using setAccessible(true) to access private fields or methods) from external modules. Deep reflective access requires the opens directive instead.
  • Intra-module Access: The exports directive has no effect on code within the same module. All packages within a single module can freely interact with each other, subject only to standard access modifiers.
Master Java with Deep Grasping Methodology!Learn More