J.IX · Java 25 practice question
A web framework binds a ScopedValue in serve, calls user code, and the user code calls back into the framework's readKey method on the same thread. What does readKey observe?
Check answer and explanation
Correct answer: The value bound by serve earlier in the thread
Explanation
Scoped values provide one-way data sharing down the call stack, so readKey observes the value bound by serve earlier in the thread even when invoked through intermediate user code.
Common trap
Intermediate user stack frames do not clear or isolate the binding; any method called directly or indirectly from run sees the bound value. The run method provides one-way sharing from caller to callee rather than two-way mutation back to the caller.
Source: JEP 506: Scoped Values — Description
Checked against the cited text on 2026-11-25.