Course contentsShow
← Java
Lesson 2483 of 2,87047. Java Platform Module System (JPMS)Free lesson

Qualified exports: Restricting Access to Specific Modules

Use exports...to syntax to make a package available only to named modules, not the entire module graph.

Qualified exports: Restricting Access to Specific Modules

You'll learn how to export a package to only specific, named modules instead of making it available to everyone.

What Is a Qualified Export?

By now you know that exports com.example.api; makes that package available to all modules that requires you. But sometimes you want more control—like a package that should only be accessible to specific, trusted modules. That's where qualified exports come in.

Think of it like a private party invitation list: instead of opening your door to the whole neighborhood, you only let in the guests whose names you've written down.

Syntax: exports...to

The syntax is straightforward:

module my.library {
    exports com.example.internal to my.app, my.tests;
}

This means com.example.internal is only accessible to modules named my.app and my.tests. No other module—even if it declares requires my.library—can see that package.

Why Use Qualified Exports?

  • Expose internals selectively for testing or tight integration without making them fully public
  • Control your API surface: keep implementation packages hidden from general consumers
  • Enable framework/library cooperation without polluting the public API

Example Module Descriptor

module data.processor {
    exports com.example.api;  // Available to everyone
    exports com.example.debug to debugger.module;  // Only debugger.module can access
}

The debugger.module can see both packages. All other modules see only com.example.api.

Key Takeaway:
Qualified exports (exports...to) let you grant package access to specific modules by name, giving you fine-grained control over who can use your internal code while maintaining strong encapsulation for everyone else.