File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments