J.VIII · Java 25 practice question
A module wants to provide an implementation for a service interface declared in a different module. Under what condition does this compile?
Check answer and explanation
Correct answer: It compiles as long as the service is accessible to code in the current module
Explanation
A provides directive may reference a service declared in another module, provided that the service is accessible to code in the current module; otherwise, a compile-time error occurs.
Common trap
Service providers are commonly split into separate modules from the service API, so cross-module service provision is fully supported. Any accessible module can declare the service, not just java.base.
Source: JLS SE 25 §7.7.4. Service Provision
Checked against the cited text on 2026-11-25.