Java 25 introduces the Stable Value API (JEP 502) to solve the trade-off between immutability and delayed initialization. StableValue acts as a thread-safe wrapper that guarantees single initialization while enabling deferred computation of expensive objects. The API includes StableSupplier for lazy initialization, StableList and StableMap for collections with on-demand element computation, and StableFunction for memoized function calls. This approach maintains immutability benefits and JVM optimization opportunities while avoiding upfront initialization costs that can slow application startup.
Table of contents
1. Overview2. Understanding the Problem3. What’s a StableValue?4. StableSupplier5. Stable Collections6. StableFunction7. ConclusionSort: