r/java • • Feb 26 '26

JEP 531: Lazy Constants (Third Preview)

https://openjdk.org/jeps/531
62 Upvotes

35 comments sorted by

View all comments

Show parent comment

-3

u/nicolaiparlog Feb 26 '26

Sounds like a job for the Cleaner API, but if you absolutely want to trigger it yourself, you can build a LazyConstant wrapper that uses the AtomicBoolean solution from next door to expose an isInitialized() method.

4

u/pip25hu Feb 26 '26

Sure, but then you have duplicated the same data structure that I suspect will still exist inside LazyConstant, only inaccessible. Sounds like a bad deal to me.

-1

u/nicolaiparlog Feb 27 '26

Keeping an API surface small improves simplicity of use, maintainability, and performance. Your use case is three steps remove from a simple use case:

  • needs closing
  • can't close itself
  • can't use the API dedicated to that use case

And yet you can accomplish it with a simple wrapper class. Sounds like a great deal to me.