Sub topic 4
Declaring a ScopedValue
This chapter assumes you know core Java syntax, including lambdas, and that you understand the call stack and static final fields. Familiarity with ThreadLocal helps but isn't required.
A ScopedValue is a key
A ScopedValue<T> is a key, not a container. The object you declare doesn't hold a value. It identifies a slot, and the value arrives later, when code binds the key for the duration of a call. Until then the key has no value.
You can't create one with new. You call the factory method ScopedValue.newInstance():
static final ScopedValue<String> USER = ScopedValue.newInstance();The type argument fixes what can be bound. USER accepts only String values, and get() returns a String with no cast.
Why static final
Declare scoped values as static final fields. The reasons are practical.
- Code far apart in the call chain must refer to the same key. A shared constant is the simplest way to do that.
- Binding and reading match on key identity. A different
ScopedValueinstance is a different slot, even if the type and purpose are the same. - The key never changes. Only the binding changes, and only inside a scope.
The field's visibility controls who can use the value. A public key lets any code bind and read it. A private key keeps both inside one class. A common design is a private key with a public static method that only reads it.
public class RequestContext {
static final ScopedValue<String> USER = ScopedValue.newInstance();
public static void main(String[] args) {
ScopedValue.where(USER, "alice").run(() -> handle());
}
static void handle() {
System.out.println("Handling request for " + USER.get());
}
}handle() takes no parameters, yet it reads "alice". The main method binds USER with where(...).run(...). The binding exists while run executes and ends when it returns. Later chapters cover binding and reading in detail. Here the focus is the declaration.
Running the example
Scoped values are final in Java 25. On that release, run the file directly:
// Save the class above as RequestContext.java, then run:
// java RequestContext.javaOn Java 21 through 24 the API is a preview feature. Add the preview flags:
// java --enable-preview --source 21 RequestContext.java
// Use the --source value that matches your JDK, for example 24.Declaration mistakes
- Not static. An instance field works technically, but each object then has its own key, so other code can't find the binding without a reference to that object.
- Not final. If a non-final field is reassigned, code can end up holding the old key and reading an unbound slot.
- Expecting a default.
newInstance()takes no initial value. A fresh key is unbound, and callingget()on it throws an exception. The chapter on unbound access shows how to handle that. - Mutable value types. The binding is fixed for the scope, but if
Tis a mutable object, its contents can still change. Prefer immutable types such asString, records, or unmodifiable collections.
Exercise
Why is a ScopedValue normally declared as private static final or public static final, and what does the declaration itself contain?
Show answer
A static final field gives all code the same key object, because bindings are matched by key identity, and the visibility modifier controls which code may bind or read it. The declaration holds only the key and its type from ScopedValue.newInstance(). It holds no value, because values come from where(...).run(...).