Home > Articles

This chapter is from the book

9.12 Qualified Exporting and Opening

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 java.base module contains a statement

exports sun.net to
  java.net.http,
  jdk.naming.dns;

Such a statement is called a qualified export. The listed modules can access the exported 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 sun.net package is placed inside the java.base module because that is where it is mostly needed. However, a couple of other modules also use that package. The Java platform designers didn't want to make java.base even bigger, and they didn't want to make the internal sun.net package generally available. In a greenfield project, one can instead design a more modular API.

Similarly, you can restrict the opens statement to specific modules. For example, in Section 9.7, “Modules and Reflective Access,” on p. 515 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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.