J.VIII · Java 25 practice question

A developer writes `provides com.acme.LogLevel with com.acme.CustomLevel;` in a module declaration, where `LogLevel` is an `enum` class. What is the result?

Choose one answer
Check answer and explanation

Correct answer: A compile-time error occurs because a provides directive cannot specify an enum class as the service

Explanation

Specifying an enum class as the service in a provides directive is explicitly a compile-time error.

Common trap

Enum classes cannot be subclassed in normal code anyway, and the module system explicitly rejects enum classes as services at compile time. The error is caught during compilation rather than deferred to ServiceLoader at run time.

Source: JLS SE 25 §7.7.4. Service Provision
Checked against the cited text on 2026-11-25.

Report an error in this question