Return to site

☕🎓JAVA CERTIFICATION QUESTION: JPMS directive and reflection

March 2, 2024

Which directive, as outlined in the text, allows other modules to employ reflection on types within a designated package while abstaining from their usage during compile time?

* exports

* opens

* requires

* uses

* provides ... with

#java #certificationquestion #ocp

https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

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.

A key motivation of the module system is strong encapsulation.

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:

opens package

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:

opens package to comma-separated-list-of-modules

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: