JEP proposed to target JDK 26: 526: Lazy Constants (Second Preview)
Sergey Bylokhov
bylokhov at amazon.com
Fri Nov 7 01:23:07 UTC 2025
Hello,
It might be worth clarifying in the JEP text that a LazyConstant field should also be declared as final.
For example, consider this case:
private LazyConstant<OrderController> ORDERS = LazyConstant.of(...);
The value is initialized lazily, but since ORDERS isn’t final, can the JVM reliably inline it?
In the “Flexible initialization with lazy constants” section, the table currently lists and compares
three cases:
1. "final field"
2. "LazyConstant"
3. "non-final field"
It might be useful to clarify that a LazyConstant field has all the benefits of a final field only
if the field itself is declared as final. Otherwise, it behaves more like a non-final field, despite
using a LazyConstant object.
BTW how is it expected to work if the LazyConstant is created and stored in a local variable?
Also, there’s a small cosmetic issue in the code examples, the use of the private modifier is
inconsistent. In some examples it is included, while in others it is omitted.
--
Best regards, Sergey.
More information about the jdk-dev
mailing list