On Mon, 13 Oct 2025 14:23:40 GMT, Per Minborg <[email protected]> wrote:
>> src/java.base/share/classes/java/lang/LazyConstant.java line 192:
>>
>>> 190: * The {@code LazyConstant} type is not {@link Serializable}.
>>> 191: * <p>
>>> 192: * It is not recommended putting lazy constants into
>>> equality-based collections
>>
>> Not sure what you mean here. It seems like the javadoc for `equals` says
>> that it triggers initialization, so it should be ok?
>
> Formally, it is ok, but may result in strange side effects if we, for
> example, put a key of type LazyConstant in a map. It will then be initialized
> immediately (upon `map.put()`), and the put operation can therefore have
> non-obvious side effects.
I think it would be better to link to `equals` more explicitly -- e.g. "As
equals and hashCode might trigger the initialization of a lazy constant, it is
recommended to avoid storing lazy constants into equality-based collections (or
similar constructs) as their addition (e.g. via `Map::put`, or `Set::add`)
might result in subtle side-effects).
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/27605#discussion_r2426535831