Qualified Export in Java Modules

Overview

Qualified exports in Java modules provide a mechanism to export a package to specific modules, rather than making it universally accessible. This feature enhances strong encapsulation and API control by allowing internal APIs to be shared among a select group of modules while keeping them hidden from others. This selective exposure is crucial for maintaining the integrity and security of a module's internal workings.

Exposing Internal APIs

Qualified exports are particularly useful for exposing internal APIs to specific modules without making them available to all. This approach is beneficial for sharing functionality among a set of modules while maintaining encapsulation. By using qualified exports, developers can ensure that only privileged modules have access to certain packages, thereby controlling the distribution and usage of internal APIs.

Exporting Packages to Specific Modules

The exports to directive is used to implement qualified exports. This directive allows a package to be exported to specific modules, restricting access to only those named modules. This ensures that the package remains encapsulated from other modules, thereby preventing unauthorized access and maintaining the module's integrity.

When to Use Qualified Exports

Qualified exports should be employed when there is a need to share a package between specific modules without exposing it to all modules. This is particularly useful in the context of libraries, frameworks, and large applications where controlling API access is crucial. By using qualified exports, developers can prevent direct dependencies on specific implementations, although it may introduce coupling to a specific module.

Managing Qualified Exports on the Command Line

Qualified exports can also be managed on the command line using the --add-exports option. This option allows a package to be exported to specific modules at both compile and run time. This capability is useful for accessing internal APIs when necessary, providing flexibility in how modules interact with each other.

Visual Representation

Figure 11.14 The module owner uses a qualified export to make the package pack accessible only to the privileged module. To privileged, it’s just as accessible as if a regular export were used; but other modules, like regular, can’t access it. Figure 11.14 The module owner uses a qualified export to make the package pack accessible only to the privileged module. To privileged, it’s just as accessible as if a regular export were used; but other modules, like regular, can’t access it.

This figure illustrates how a module owner can use a qualified export to make a package accessible only to a privileged module. For the privileged module, the package is as accessible as if a regular export were used, but other modules, such as a regular module, cannot access it. This visual representation underscores the selective nature of qualified exports in Java modules.

FAQ (Frequently asked questions)