J.IX · Java 25 practice question

What does the following Java 25 program print when a helper method reads a `ScopedValue` bound by its caller?

static final ScopedValue<String> TRACE = ScopedValue.newInstance();
String readTrace() { return TRACE.get(); }
void main() {
    ScopedValue.where(TRACE, "tx-9").run(() -> System.out.println(readTrace()));
}
Choose one answer
Check answer and explanation

Correct answer: tx-9

Explanation

`readTrace()` is a legitimate callee invoked within the `where(...).run(...)` scope, so `TRACE.get()` returns `tx-9`.

Common trap

The value is bound for the dynamic extent of `run`, so `readTrace()` retrieves `tx-9` rather than `null` or throwing.

Source: JEP 506: Scoped Values — Goals
Checked against the cited text on 2026-11-27.

Report an error in this question