Qualified Exporting and Opening in Java

In this section, you will see a variant of the exports and opens statement that narrows their scope to a specified set of modules. For example, the javafx.base module contains a statement

exports com.sun.javafx.collections to

javafx.controls, javafx.graphics, javafx.fxml, javafx.swing;

Such a statement is called a qualified export. The listed modules can access the package, but other modules cannot.

Excessive use of qualified exports can indicate a poor modular structure. Nevertheless, they can arise when modularizing an existing code base. Here, the Java platform designers distributed the code for JavaFX into multiple modules, which is a good idea because not all JavaFX applications need FXML or Swing interoperability. However, the JavaFX implementors liberally used internal classes such as com.sun.javafx.collections.ListListenerHelper in their code. In a greenfield project, one can instead design a more robust public API.

Similarly, you can restrict the opens statement to specific modules. For example, in Section 9.7, “Modules and Reflective Access,” on p. 511 we could have used a qualified opens statement, like this:

module v2ch09.openpkg

{

requires com.horstmann.util;

opens com.horstmann.places to com.horstmann.util;

}

Now the com.horstmann.places package is only opened to the com.horstmann.util module.


Source: Horstmann Cay S. (2019), Core Java. Volume II – Advanced Features, Pearson; 11th edition.

Leave a Reply

Your email address will not be published. Required fields are marked *