JEP 526 renames Java's Stable Values feature to Lazy Constants, introducing the LazyConstant<T> container as a preview feature in Java 26. A LazyConstant holds a single value that becomes immutable once set, enabling lazy initialization without sacrificing JVM optimizations like constant folding. Key changes from Java 25 include: removal of the empty of() constructor (a supplier must now be provided), consolidation of multiple setter methods into a single orElse method, removal of Stable Functions, and migration of stable collections to List.ofLazy() and Map.ofLazy() factory methods. Potential pitfalls include lack of Serializable support and the need to manually declare the LazyConstant field as final to prevent reassignment.
Table of contents
What are Lazy ConstantsBefore Lazy ConstantsLet's use some Lazy ConstantsWhat has changed in Java 26?Potential issuesWhat is the goal of this JEP?Sort: