Before Java 9, reflection could be used to learn about all types in a package and all members of a type—even its private members.
Thus, nothing was truly encapsulated.
By default, a type in a module is not accessible to other modules unless it’s a public type and you export its package.
You expose only the packages you want to expose.
With Java 9, this also applies to reflection.
Allowing runtime-only access to a package.
An opens module directive of the form:
indicates that a specific package’s public types (and their nested public and protected types) are accessible to code in other modules at runtime only.
Also, all the types in the specified package (and all of the types’ members) are accessible via reflection.
Allowing runtime-only access to a package by specific modules.
Use an opens…to module directive of the form:
Allowing runtime-only access to all packages in a module.
If all the packages in a given module should be accessible at runtime and via reflection to all other modules, use: