Skip to content

Commit 7b3aafd

Browse files
committed
JEP 506: Scoped Values
1 parent 59ecf23 commit 7b3aafd

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ This repository contains Java examples that are designed to track and document t
1111
## Specifications & Practices
1212

1313
* [Java 25](java-25/) (September, 2025)
14-
* [JEP 513](java-25/src/main/java/JEP513FlexibleConstructorBodies.java): Flexible Constructor Bodies
15-
* [JEP 512](java-25/src/main/java/JEP512CompactSourceFilesAndInstanceMainMethods.java): Compact Source Files and Instance Main Methods
14+
* [JEP 513](java-25/src/main/java/JEP513FlexibleConstructorBodies.java): Flexible Constructor Bodies
15+
* [JEP 512](java-25/src/main/java/JEP512CompactSourceFilesAndInstanceMainMethods.java): Compact Source Files and Instance Main Methods
1616
* [JEP 511](java-25/src/main/java/JEP511ModuleImportDeclarations.java): Module Import Declarations
17+
* [JEP 506](java-25/src/main/java/JEP506ScopedValues.java): Scoped Values
1718

1819
* [Java 24](java-24/) (March, 2025)
1920
* [JEP 488](java-24/src/main/java/JEP488PrimitiveTypesInPatternsInstanceofAndSwitch.java): Primitive Types in Patterns, instanceof, and switch
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import static java.lang.ScopedValue.where;
2+
3+
static final ScopedValue<String> CONTEXT = ScopedValue.newInstance();
4+
5+
// JEP 506: Scoped Values
6+
// https://openjdk.org/jeps/506
7+
8+
void main() {
9+
where(CONTEXT, "CTX-12345").run(() -> {
10+
foo();
11+
bar();
12+
});
13+
14+
/*
15+
foo sees context: CTX-12345
16+
bar sees context: CTX-12345
17+
bar->nested sees: CTX-OVERRIDE
18+
bar after nested sees: CTX-12345
19+
* */
20+
}
21+
22+
static void foo() {
23+
System.out.println("foo sees context: " + CONTEXT.get());
24+
}
25+
26+
static void bar() {
27+
System.out.println("bar sees context: " + CONTEXT.get());
28+
// Nested binding
29+
where(CONTEXT, "CTX-OVERRIDE").run(() -> {
30+
System.out.println("bar->nested sees: " + CONTEXT.get());
31+
});
32+
System.out.println("bar after nested sees: " + CONTEXT.get());
33+
}

0 commit comments

Comments
 (0)