Class ScopedValuesExamples

java.lang.Object
net.jrodolfo.java_evolution.java25.ScopedValuesExamples

public class ScopedValuesExamples extends Object
Demonstrates scoped values, finalized in Java 25 by JEP 506.

Scoped values solve a common context-passing problem. Data such as a user, request ID, or tenant often needs to be available through a call chain without adding parameters everywhere. Compared with mutable thread-local state, scoped values bind immutable data to a bounded dynamic scope, making cleanup and ownership easier to reason about.

  • Constructor Details

    • ScopedValuesExamples

      public ScopedValuesExamples()
  • Method Details

    • userInsideScope

      public String userInsideScope(String user) throws Exception
      Binds a scoped value for the duration of one call.
      Parameters:
      user - user name to bind
      Returns:
      value observed inside the scope
      Throws:
      Exception - when the scoped call fails
    • userIsBoundOutsideScope

      public boolean userIsBoundOutsideScope()
      Checks whether the scoped value is bound outside a scope.
      Returns:
      whether the scoped value is currently bound