J.IX · Java 25 practice question

What does the following Java 25 program print when a nested `ScopedValue.where` rebinds a value inside an outer scope?

void main() {
    ScopedValue<String> ROLE = ScopedValue.newInstance();
    ScopedValue.where(ROLE, "user").run(() -> {
        ScopedValue.where(ROLE, "admin").run(() -> System.out.print(ROLE.get() + "-"));
        System.out.print(ROLE.get());
    });
}
Choose one answer
Check answer and explanation

Correct answer: admin-user

Explanation

Inside the inner scope `ROLE` is `admin`, and once that scope ends `ROLE` reverts to `user`, printing `admin-user`.

Common trap

Rebinding in a nested scope does not overwrite the outer scope's binding once the inner scope exits.

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

Report an error in this question