J.VI · Java 25 practice question
A developer tries to define a custom subclass of ScopedValue to add logging hooks. What happens when this Java 25 code is compiled?
class CustomScope<T> extends ScopedValue<T> {
}
class Demo {
public static void main(String[] args) {
System.out.println("ok");
}
}
Check answer and explanation
Correct answer: Compilation fails because ScopedValue is declared final and cannot be subclassed.
Explanation
ScopedValue<T> is declared as a public final class in java.lang, so attempting to extend it causes a compile-time error.
Common trap
Expecting compilation to succeed is tempting because ThreadLocal can be subclassed (for example to override initialValue), but ScopedValue is final. Blaming a missing get() override is tempting if someone thinks ScopedValue is abstract, yet the class is final and cannot be extended regardless of methods overridden.
Source: Java SE 25 API: ScopedValue
Checked against the cited text on 2026-11-25.