Multiple Qualified exports for Fine-Grained Control
You'll learn how to export the same package to multiple specific modules by declaring multiple qualified exports...to statements.
The Core Idea
Sometimes you need to share a package with several trusted modules, but not the entire world. Rather than using a single unqualified exports (which makes the package public to everyone), you can write multiple qualified exports...to declarations for the same package—each targeting a different specific module.
Think of it like a private club with an invitation list: instead of opening your doors to the public, you hand-pick multiple guests by name. Each exports...to line adds another module to your approved list.
Syntax
In your module-info.java, you can repeat the exports...to directive for the same package:
module my.library {
exports com.mylib.internal to module.one;
exports com.mylib.internal to module.two;
exports com.mylib.internal to module.three;
}
Each line grants visibility of com.mylib.internal to exactly one additional module. Modules module.one, module.two, and module.three can now access com.mylib.internal, but all other modules cannot.
When to Use This
- Framework internals: Share utility packages with your framework's companion modules without exposing them publicly.
- Team boundaries: Allow specific modules maintained by your team to collaborate while hiding implementation details from external modules.
- Security: Limit sensitive API surface area to only the modules that absolutely need it.
Key Takeaway:
Use multiple exports...to declarations for the same package to grant fine-grained visibility to several specific modules, maintaining strong encapsulation while enabling controlled collaboration.