Estimate project
← All posts

JEP 531 - Lazy Constants, the journey goes on

A look at the Lazy Constants feature from JEP 531 in Java 27: API changes, error handling, lazy collections and known limitations.

5 min read
JavauncoverjavaJava 26JEP526

Java 25 brought a new feature named Stable Values. In this post we’ll examine JEP-531, the third preview of that feature, which ships with Java 27. It remains a preview feature, so anything may still change.

With JEP-526, Stable Values turned into Lazy Constants. In my opinion, that name fits the functionality much better.

This article was revised on 15 Sept 2026.

What are Lazy Constants

A LazyConstant <T> is a container holding one value of type T. After it has been assigned, that value can no longer change. Think of it as an eventually final value. It’s important to note that it is the reference to an object that becomes immutable – the underlying object itself can still be modified.

Before Lazy Constants

Prior to Java 25, immutable data required the final keyword.

class Controller {
 private final EmailSender sender = new EmailSender();
}

That approach came with a few problems.

Any final field in our code has to be set eagerly – either initialized in a constructor or declared as a static field. This can slow down application startup. After all, not every field is needed right away, is it?

So perhaps we could drop the final keyword and write something along these lines:

class PetClinicController {

 private EmailSender sender = null;

 EmailSender getSender() {
   if (sender == null) {
     sender = new EmailSender();
   }
   return sender;
 }

 void adoptPet(User user, Pet pet) {
   // some logic here
   getSender().sendEmailTo(user, "You are great person!");
 }
}

That will work, won’t it? Startup gets faster. And indeed, this was one way of tackling the problem before Java 25. Sadly, it comes at a price.

  • The sender remains mutable; a different value can be assigned to the variable. Stopping that would require external tools or very careful code reviews.
  • A null pointer exception may occur if the field is accessed in some way other than through the getter.
  • Ideally, “getSender” should be thread-safe.
  • Yet even if we make it so, we stop the JVM from optimizing access to this field – for instance with constant-folding.

Let’s use some Lazy Constants

Let’s take the earlier example and move it to the new Java.

class PetClinicController {

 private final LazyConstant<EmailSender> sender = LazyConstant.of(() -> new EmailSender());

 EmailSender getSender() {
   return sender.get();
 }

 void adoptPet(User user, Pet pet) {
   // some logic here
   getSender().sendEmailTo(user, "You are great person!");
 }
}

The code looks much the same, but now LazyConstant takes care of EmailSender’s nullability. As a result, EmailSender cannot be called without first invoking a method that retrieves the value.

Setting the value inside a LazyConstant is guaranteed to be thread-safe.

What has changed in Java 27?

Java 26 had already made LazyConstant considerably smaller than Stable Values used to be. Java 27 keeps refining the API.

Error handling

In Java 27, LazyConstant behaves differently when the computing function fails.

In Java 26, an exception thrown by the supplier was passed on to the caller, and the LazyConstant stayed uninitialized. A later call to get() could therefore attempt to compute the value once more.

var count = new AtomicInteger();
var value = LazyConstant.of(() -> {
   if (count.getAndIncrement() == 0) {
       throw new RuntimeException("Boom!");
   }
   return "Hello!";
});

Java 26

value.get(); // throws NoSuchElementException
value.get(); // "HELLO!" - computation is retried

Java 27

value.get(); // throws NoSuchElementException
value.get(); // throws NoSuchElementException, supplier is not called again

The low-level methods isInitialized and orElse are gone

Java 27 removes both orElse and isInitialized, since they could be used in ways that went against the design goals. In Java 26, unless you invoked the get() method, there was no guarantee the value would be present by the time you read it.

In practice, the change makes the mental model simpler: you always call get(), and you always receive the value – unless an error gets thrown, as described above.

LazyConstant<String> example = LazyConstant.of(() -> "Hello");
var result = example.get();
System.out.println(result);

Here the result is "Hello", with no ”else” handling needed.

Lazy Collections

Unmodifiable collections can also be combined with stable values.
Here’s how the versions differ.

Java 25

List<Integer> list = StableValue.list(SIZE, INT_FUNCTION);
Map<Color, HowCute> map = StableValue.map(KEYS, CUTE_FUNCTION);

Java 26

List<Integer> list = List.ofLazy(SIZE,INT_FUNCTION);
Map<Color, HowCute> map = Map.ofLazy(KEYS, CUTE_FUNCTION);

Java 27

List<Integer> list = List.ofLazy(SIZE,INT_FUNCTION);
Map<Color, HowCute> map = Map.ofLazy(KEYS, CUTE_FUNCTION);
Set<Dog> set = Set.ofLazy(ALL_DOGS, IS_ADOPTED_FUNCTION);

This seems handy for complex computations. It is limited, though, since a list can take only an intFunction as input. I hope we’ll see more in the future.

Potential issues

LazyConstants are straightforward and useful constructs. Still, it pays to be aware of the pitfalls that come with them.

Serializable

Note that Lazy Constants don’t support Serializable. The JEP doesn’t say so, but you can find it here.

So if you rely on serializable and plan to swap all finals for Stable Values, you might run into trouble.

Not Using Final

Every time we use LazyConstant, we have to keep in mind that a different value could be assigned to the variable. Nothing stops us from writing something like this (apart from common sense, naturally):

private LazyConstant<EmailSender> sender = LazyConstant.of(() -> new EmailSender());

void adoptPet(User user, Pet pet) {
 sender = null;
    // some logic here
 sender.get() //Null pointer exception
}

All your LazyConstant fields should be final, but nothing forces you to make them so. It’s the Optional<> story all over again.

Why, then, didn’t they add a new keyword – something like lazy, as other languages have? Let’s look at the reasoning behind it.

What is the goal of this JEP?

The JEP tells us that the author aimed to speed up the startup of Java applications by separating the creation of lazy constants from their initialization, while ensuring correct behavior in a multi-threaded environment and enabling JVM optimizations such as constant-folding.

What they are not aiming for:

  • Extending the Java programming language with a way to declare lazy fields is not a goal.
  • Changing the semantics of final fields is not a goal.

So although I’d personally love a lazy keyword, I’m happy they’ve added something that helps us optimize code. I’m looking forward to the next updates.