Syntax of opens in module-info.java
You'll learn the exact syntax for declaring opens directives to grant reflective access to your module's packages.
The Basic Syntax
Now that you understand why opens is needed (to allow frameworks to use reflection on your internal types), let's see exactly how to write it.
The simplest form looks like this:
module com.example.myapp {
opens com.example.myapp.model;
}
This single line opens the entire com.example.myapp.model package to reflective access by all other modules at runtime.
What This Means
Think of opens as unlocking a back door. While exports controls normal compile-time and runtime access, opens specifically grants permission for reflection—allowing frameworks to call setAccessible(true) on your private fields, constructors, and methods.
Without an opens directive, reflection into non-exported packages (or even exported packages' private members) will fail with InaccessibleObjectException at runtime.
Multiple opens Declarations
You can open multiple packages:
module com.example.myapp {
opens com.example.myapp.model;
opens com.example.myapp.entities;
opens com.example.myapp.dto;
}
Each package requires its own opens directive—just like with exports.
Key Difference: opens vs exports
exports: Makes public types accessible for normal use (compile-time and runtime)opens: Allows deep reflection on all types (public and non-public), but doesn't grant normal access
A package can be both exported and opened if you need both capabilities.
Key Takeaway: Use opens <package-name>; in module-info.java to allow reflective access to all types in that package, enabling frameworks to introspect and modify your module's internals at runtime.