1Z0-819 · Question #45
Examine these module declarations: module ServiceAPI { exports com.example.api; } module ServiceProvider { requires ServiceAPI; provides com.example.api with com.myimp1.imp1; } module Consumer {…
The correct answer is A. The ServiceProvider module is the only module that, at run time, can provide the com.example.API API. C. The Consumer module should require the ServiceProvider module. There is an error in the stated answer for this question. Option C is incorrect per Java Platform Module System (JPMS) semantics, and option E is actually the second correct answer alongside A. Here is the accurate explanation: Option A is correct because, within this specific…
Question
Options
- AThe ServiceProvider module is the only module that, at run time, can provide the com.example.API API.
- BThe ServiceAPI is the only module that, at run time, can provide com.example.API, makes it easy to install multiple provider modules.
- CThe Consumer module should require the ServiceProvider module.
- DThe ServiceProvider module should export the com.mypimpl.impl package.
- EThe ServiceProvider module does not know the identity of a module (such as Consumer) that uses the com.example.API API.
How the community answered
(38 responses)- A79% (30)
- B11% (4)
- D3% (1)
- E8% (3)
Explanation
There is an error in the stated answer for this question. Option C is incorrect per Java Platform Module System (JPMS) semantics, and option E is actually the second correct answer alongside A. Here is the accurate explanation:
Option A is correct because, within this specific module configuration, ServiceProvider is the only module that declares a provides clause for com.example.api, making it the sole runtime provider in this setup. Option E is correct because the service pattern in JPMS is designed for loose coupling: the provider registers itself via provides ... with ..., and neither it nor ServiceAPI has any visibility into which modules consume the service at runtime.
Option C is wrong and is arguably the most important distractor to understand: the Consumer module should NOT require ServiceProvider. The entire purpose of the uses/provides service mechanism and ServiceLoader is to decouple the consumer from any specific implementation. Requiring the provider would break this pattern and create a hard dependency. Option D is wrong because the ServiceProvider does not need to export its implementation package com.myimpl.impl; the consumer only interacts with the interface from ServiceAPI, so the implementation can stay hidden. Option B is wrong because ServiceAPI merely exports the interface, not an implementation, and the phrasing about "installing multiple providers" is backwards from how the pattern actually works.
Memory tip: Think of the service pattern as a job board. ServiceAPI posts the job description (interface), ServiceProvider submits a resume (provides ... with ...), and Consumer posts that it is hiring (uses). The employer never needs to know which candidates exist in advance, and candidates never know who reads their resume. The key rule is that the Consumer only needs to know the API module, never the provider.
Topics
Community Discussion
No community discussion yet for this question.