Sub topic 3
Which Java versions include scoped values and how to enable them
This chapter assumes you can compile and run Java from the command line and know what a JDK release number means. Scoped values changed status several times, so the flags you need depend on your JDK.
The release history
The feature moved from incubator to preview to final:
| JDK | Status | Where the class lives | What you must enable |
|---|---|---|---|
| 20 | Incubator | `jdk.incubator.concurrent` | `--add-modules jdk.incubator.concurrent` |
| 21 to 24 | Preview | `java.lang` | `--enable-preview` |
| 25 and later | Final | `java.lang` | Nothing |
JDK 25 is a long-term support (LTS) release. It is the first LTS in which ScopedValue is a permanent API. JDK 21 is also LTS, but there the API is only a preview.
Using the preview API on JDK 21 to 24
A preview API is complete and supported, but its signatures may still change in the next release. The compiler and runtime both require an explicit opt-in. Save this as RequestContext.java:
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());
}
}On JDK 21, compile and run it like this:
javac --release 21 --enable-preview RequestContext.java
java --enable-preview RequestContextThe --release value must match the JDK you are running. Classes compiled with preview features are tied to that exact version, so a class compiled on JDK 21 will not load on JDK 22. Rebuild after every JDK upgrade.
You can also run the source file directly:
java --enable-preview --source 21 RequestContext.javaWithout the flag, javac rejects the code with a message saying that ScopedValue is a preview API and is disabled by default.
Using it on JDK 25 and later
Compile and run normally:
javac RequestContext.java
java RequestContextNo flags are needed, and the example above compiles unchanged.
Build tools
In Maven, set the compiler plugin's <release> and add --enable-preview to <compilerArgs>. Add the same flag to the Surefire argLine so tests can run. In Gradle, add --enable-preview to options.compilerArgs and to the jvmArgs of your Test and JavaExec tasks. Forgetting the runtime flag is the usual cause of an UnsupportedClassVersionError that mentions preview features.
Checking your setup
Run java --version to see the JDK. If it is 25 or later, you need nothing more. If it is 21 to 24, add --enable-preview. If it is 20, the API is in a different package, and its method names differ slightly from what this tutorial uses. Prefer upgrading to 21 or later.
Exercise
You compile a class that uses ScopedValue on JDK 22 with javac --release 22 --enable-preview. A colleague runs it on JDK 23 with java --enable-preview. What happens, and what is the fix?
Show answer
The JVM refuses to load the class, because preview class files only run on the exact JDK version that compiled them. The fix is to recompile with --release 23 --enable-preview, or to move both machines to JDK 25, where scoped values are final and no flag is needed.