Java Performance Tuning
Java(TM) - see bottom of page
Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks!
Training online: Concurrency, Threading, GC, Advanced Java and more ...
Tips February 2026
|
JProfiler
|
|
Get rid of your performance problems and memory leaks!
|
|
JProfiler
|
|
Get rid of your performance problems and memory leaks!
|
|
|
Back to newsletter 303 contents
https://www.youtube.com/watch?v=BZlXZyXA4jY
LazyConstants in JDK 26 (Page last updated February 2026, Added 2026-02-26, Author Nicolai Parlog, Publisher Inside Java Newscast). Tips:
- final LazyConstant<T> myfield; myfield = LazyConstant.of(T::initialize); ... later ... myfield.get().
- Only the first call to LazyConstant.get() does any work (if successful). It handles concurrency and every subsequent call just returns the constant - and the implementation is designed for the JVM to be able to easily optimize the access.
- Use LazyConstant.of(Supplier<T>) to define the computation and LazyConstant.get() to retrieve the value.
- LazyConstant leverages the @stable annotation, which informs the Java runtime that the field will not be reassigned, enabling constant folding optimizations. Unlike standard final instance fields, which can sometimes be changed via reflection, LazyConstant values are treated as truly constant by the JVM.
- If LazyConstant.get() throws an exception, the supplier for the get() will be retried on subsequent calls. Circular dependencies will throw an IllegalStateException to prevent infinite loops.
- Lazy collections are supported, specifically List.ofLazy and Map.ofLazy, which compute individual elements or values only when first accessed. These lazy collections are unmodifiable, thread-safe, and support constant-folding optimizations.
Jack Shirazi
Back to newsletter 303 contents
Last Updated: 2026-02-26
Copyright © 2000-2026 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
URL: http://www.JavaPerformanceTuning.com/news/newtips303.shtml
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us