Fundamentals 11 min read

JDK 27 Trims Two API Methods: Introducing JEP 531 and Set.ofLazy()

JDK 27’s third preview (JEP 531) removes the isInitialized() and orElse() methods from the LazyConstant API, adds a Set.ofLazy() factory method, and refines the lazy‑constant design to improve composability, startup performance, thread safety, and JVM optimizations.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
JDK 27 Trims Two API Methods: Introducing JEP 531 and Set.ofLazy()

Why the JEP number changed from 526 to 531?

The JEP document states that the same feature has been previewed three times, each time receiving a new JEP number because a substantive API revision triggers a new identifier. JDK 25 introduced StableValue (JEP 502), JDK 26 renamed it to LazyConstant (JEP 526) with a major redesign, and JDK 27 refines it further under JEP 531.

Core changes in the third preview

The update performs both a subtraction and an addition:

Removed methods: isInitialized() and orElse(), because they break the API’s design goal of transparent lazy initialization.

Added factory method: Set.ofLazy(), completing lazy support for the three fundamental collection types ( List, Set, Map).

Design rationale

The lazy‑constant philosophy is “deferred immutability”: a value is undefined until first access, after which it becomes immutable and the initialization is invisible to callers. The get() method embodies this principle, while isInitialized() and orElse() expose internal state and encourage non‑deterministic patterns, which the redesign eliminates.

Example: Set.ofLazy()

class Application {
    enum Option { VERBOSE, DRY_RUN, STRICT }
    // Lazy evaluation of option membership
    static final Set<Option> OPTIONS =
        Set.ofLazy(EnumSet.allOf(Option.class), Application::isEnabled);

    private static boolean isEnabled(Option option) {
        // parse command line, read config, query DB …
        return ...;
    }

    public static void process() {
        if (OPTIONS.contains(Option.DRY_RUN)) {
            return; // isEnabled(DRY_RUN) runs only if queried
        }
        // ...
    }
}

This example shows that each element’s membership is stored in an independent LazyConstant and evaluated only when contains() is called, guaranteeing at most one computation per option and allowing the JVM to apply constant‑folding optimizations.

Why add ofLazy() factory methods?

Beyond the single‑element LazyConstant.of(), collection factories such as List.ofLazy() and Map.ofLazy() enable composable lazy initialization. For an object‑pool scenario, List.ofLazy() replaces manual array management with concise code and lets the JVM inline each lazily created element because they are stored in @Stable fields.

static final List<OrderController> ORDERS =
    List.ofLazy(POOL_SIZE, _ -> new OrderController());

Practical impact for developers

Startup performance improves because components are initialized only when actually used.

Thread‑safety is guaranteed by @Stable annotations and memory barriers, eliminating the need for double‑checked locking or volatile fields.

The JVM can perform aggressive constant‑folding and inlining for final lazy constants, reducing method‑call overhead.

Summary and outlook

JEP 531’s third preview demonstrates that the lazy‑constant feature is maturing: the API is now minimal, the three core collection types all provide ofLazy() factories, null values are prohibited, and all methods that could compromise transparent laziness have been removed.

Following OpenJDK’s usual cadence of 2‑4 preview rounds, we can expect the feature to become a standard part of the JDK in version 28 or 29, offering Java developers a declarative, thread‑safe, and JVM‑optimizable way to implement lazy loading without manual double‑checked locking or volatile fields.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaAPI changesLazyConstantJDK 27JEP 531Set.ofLazy
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.