JDK 26 introduces LazyConstant (JEP 526) as a preview feature, replacing the JDK 25 'stable values' API. LazyConstant<T> allows fields to be lazily initialized via a Supplier while guaranteeing the value is constant once set, enabling JVM constant-folding optimizations. Key behaviors include thread-safe single initialization, null rejection, retry on supplier exception, and cycle detection. JEP 526 also adds List.ofLazy and Map.ofLazy for per-element lazy collections. The post traces the API's evolution from internal @Stable annotations through stable values in JDK 25 to the cleaner LazyConstant concept in JDK 26, with further API trimming planned for JDK 27.

11m read timeFrom nipafx.dev
Post cover image
Table of contents
▚ Laziness▚ LazyConstant▚ LazyConstant Behavior▚ Lazy Collections▚ API Evolution

Sort: