<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Hi Sergey<br>
</p>
<div class="moz-cite-prefix">On 07/11/2025 01:23, Sergey Bylokhov
wrote:<br>
</div>
<blockquote type="cite" cite="mid:b2b45136-6ab5-4ad7-9d3e-149666f3cc34@amazon.com">Hello,
<br>
<br>
It might be worth clarifying in the JEP text that a LazyConstant
field should also be declared as final.
<br>
<br>
For example, consider this case:
<br>
<br>
private LazyConstant<OrderController> ORDERS =
LazyConstant.of(...);
<br>
<br>
The value is initialized lazily, but since ORDERS isn’t final, can
the JVM reliably inline it?
<br>
<br>
In the “Flexible initialization with lazy constants” section, the
table currently lists and compares three cases:
<br>
<br>
1. "final field"
<br>
2. "LazyConstant"
<br>
3. "non-final field"
<br>
<br>
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.
<br>
</blockquote>
<p>I think there's enough bread crumbs for the readed.</p>
<p>The table compares how many times, and when assignment occurs. No
need to mention final here.</p>
<p>The first part that brings up constant folding is the para that
starts with:</p>
<p>> There is, furthermore, mechanical sympathy...</p>
<p>This para includes this sentence:<br>
</p>
<p>> This allows the JVM to treat the content of a lazy constant
as a true constant, provided that the field which refers to the
lazy constant is <code>final</code></p>
<p>Which seems "close enough" to the performance claim.</p>
<p>Also, in the "Risks and assumptions" section at the end of the
JEP, there is a further note:</p>
<p>> The JVM can apply constant-folding optimizations only when
it can trust that <code>final</code> fields can be updated only
once.</p>
<p><br>
</p>
<blockquote type="cite" cite="mid:b2b45136-6ab5-4ad7-9d3e-149666f3cc34@amazon.com">BTW how
is it expected to work if the LazyConstant is created and stored
in a local variable?
<br>
</blockquote>
I don't know if we have benchmark for this. Per do you have
anything?<br>
<blockquote type="cite" cite="mid:b2b45136-6ab5-4ad7-9d3e-149666f3cc34@amazon.com">
<br>
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.
<br>
</blockquote>
<p>Thanks, we will fix.</p>
<p>Cheers<br>
Maurizio<br>
</p>
</body>
</html>