Java SE 11 Developer (1Z0-819) — Question 98
Which code fragment does a service use to load the service provider with a Print interface?
Answer options
- A. private java.util.ServiceLoaderloader = ServiceLoader.load(Print.class)
- B. private Print print = new com.service.Provider.PrintImpl();
- C. private java.util.ServiceLoaderloader = new java.util.ServiceLoader<>()
- D. private Print print = com.service.Provider.getInstance();
Correct answer: A
Explanation
Option A is correct because it correctly uses ServiceLoader to load the service provider for the Print interface. Options B and D instantiate the Print implementation directly, bypassing the service loading mechanism, while option C incorrectly initializes a new ServiceLoader without specifying the Print interface.