r/java • u/davidalayachew • Feb 05 '26
LazyConstants in JDK 26 - Inside Java Newscast #106
https://www.youtube.com/watch?v=BZlXZyXA4jY
67
Upvotes
r/java • u/davidalayachew • Feb 05 '26
12
u/larsga Feb 05 '26
Title made me curious, but not enough to watch a video. Javadoc explains well.
7
u/0xffff0001 Feb 05 '26
I wish they would simply allow
private final lazy Log log = Log.get();
3
u/blobjim Feb 05 '26 edited Feb 05 '26
Aw I liked the StableValue name.
Also a little worried they're removing orElse. That's going to remove use-cases right? It's nice being able to create a StableValue without setting it to anything. And they already removed orElseSet???
There's already a bunch of APIs that I think would want orElseSet for efficient constants. Like the KeyStore.init method which you call after object creation. It would be nice for an implementation to set a LazyConstant in init and have it potentially inlinable.
1
u/age_of_empires Feb 06 '26
So it's a Singleton? Why not just say that
2
u/_predator_ Feb 05 '26
So something like Guava‘s memoize?
https://guava.dev/releases/19.0/api/docs/com/google/common/base/Suppliers.html#memoize(com.google.common.base.Supplier)
2
u/Brutus5000 Feb 06 '26
I wonder how this could be worked into Dependency Injection libraries. Lazy loading there is always a thing, where the frameworks are using all kind of ways to achieve it today.
-2
u/rzwitserloot Feb 05 '26
This is less useful than it looks. In that it enables common useless drivel.
This:
is probably overengineered and should just be:
The example specifically went with a non-
staticlogger which is.. a bit odd, the logger did not appear to contain any instance-specific anything, and this lazy constant stuff has only one purpose (optimisation), so, that's a bizarre example.The point is - this second snippet still only loads that logger when it is needed. Or rather, when any code anywhere actually ends up 'touching' Component. if no code ever does, even in the second snippet, the logger is never loaded.
So, the above 2 snippets (other than the bizarre
staticthing) have no difference unless your JVM ends up interacting withComponent(the type), but not invokingprocess().Which happens, but is fairly rare. Most attempts to write lazy getters should just be.. a field. With no weird initialization rituals.
And if you must have it, while this is perhaps 'cleaner', this is the old way to do that and it has allll the advantage of this new thing:
Now the logger is never initialized.. unless you call
process()in which case it is guaranteed loaded exactly once (if multiple threads callprocess()simultaneously, they will wait), and it's the most efficient the JVM is ever going to get, given that 'wait for class loading' happens, literally, to all java apps.Effective java has a chapter on this IIRC.
I'm sure I'm missing something, but, this is a neat feature that should come up virtually never, and I'm confused as to the excitement about it. The one and only thing it does is replace the somewhat esoteric 'inner class loader' pattern. Worth... something, I guess.