66.1k views
2 votes
Module A has a package called A, and module I wants to access that package. What is the minimum configuration required to achieve the functionality?

(a) Import module A
(b) Include module I
(c) Use an interface
(d) Extend module A

User Privateace
by
8.4k points

1 Answer

5 votes

Final answer:

To allow module I to access a package from module A, module A must export the package and module I must require module A in their respective module-info.java files. This configuration ensures proper module visibility and encapsulation in the Java Platform Module System.

Step-by-step explanation:

To allow module I access to a package within module A, you need to configure module A to export the package and module I to require module A. Module A's module-info.java should declare an exports statement for the specific package it intends to make available to other modules. On the other hand, module I's module-info.java should include a requires statement indicating its dependency on module A.

In terms of module visibility and encapsulation, the Java Platform Module System (JPMS) introduced in Java 9 enforces strict rules. Thus, simply having module A on the module path is not enough for module I to access its contents. Both modules must be explicitly configured with the correct directives in their module descriptor files.

Here is an example configuration:

Module A's module-info.java:


module A {
exports com.example.packageA;
}

Module I's module-info.java:

module I { requires A;
}

User Ari Seyhun
by
8.0k points