Lazy constants in Java 27


Two changes in the Java ecosystem look unrelated at first glance: lazy constants and ZGC. One is about when values get created. The other is about when memory gets reclaimed. But both are really about the same thing: moving cost away from the hot path without hiding the consequences.

That is the hard part of language and runtime design. If a default is too eager, startup and initialization pay for work that may never matter. If a runtime is too aggressive about stopping the world, latency-sensitive applications pay for pauses they cannot afford. Lazy constants and ZGC are both responses to that problem.

Lazy constants: only pay the price when using the value

Lazy constants are attractive because they let code describe a value without forcing immediate initialization. That can help with start-up time, avoid unnecessary work, and reduce awkward initialization order problems. In a large codebase, that matters a lot: not every constant-like value needs to exist the moment a class is loaded.

This lazy-ness in loading can help speed up the application start-up, especially for heavy and resource intensive code that only needs to get loaded once.

 1public class Main {
 2
 3    private static final LazyConstant<String> MESSAGE =
 4            LazyConstant.of(() -> {
 5                System.out.println("Initializing constant...");
 6                return "Hello, World!";
 7            });
 8
 9    public static void main(String[] args) {
10        System.out.println("Application started");
11
12        System.out.println(MESSAGE.get());
13        System.out.println(MESSAGE.get());
14        System.out.println(MESSAGE.get());
15    }
16}

The promise is simple, but the trade-offs are not. Laziness can make code harder to reason about because the cost moves from application start-up to first use. It also changes failure modes: initialization bugs may now appear later, in a different thread, or only under specific traffic patterns.

That makes lazy constants a design choice rather than a free win. They improve ergonomics when the value is expensive, optional, or rarely used, but they also need clear semantics around thread safety, visibility, and failure handling.

The new ZGC: keep pauses small; even with larger heaps

ZGC takes the same “move the cost elsewhere” idea and applies it to garbage collection. Instead of letting stop-the-world pauses dominate latency, it does most of its work concurrently. The result is a collector designed for responsiveness, especially when heap sizes grow and predictable pause times matter more than raw throughput.

For services that care about tail latency, that can be the difference between a smooth system and one that periodically stutters. ZGC does not remove GC work; it changes when and how the work is paid for.

That shift comes with its own costs. Concurrent GC means extra runtime sophistication and some overhead in memory management. As with lazy constants, the important question is not whether there is a cost, but where the cost lands and whether that location is acceptable for the workload.

The shared lesson

Lazy constants and ZGC belong to the same design conversation. Both try to make Java more practical for real systems by reducing upfront cost and smoothing runtime behavior. Both also force developers to think more carefully about hidden work.

That is why changes like these are controversial. They are not just new features. They change expectations: when does code run, when does memory move, and where do performance surprises show up?


See also