JDK 25 Arrives with Stable Values API and More: What’s New and Why It Matters

Java continues to evolve, and the upcoming JDK 25 release is packed with updates designed to make coding easier and applications run faster. A standout new feature is the Stable Values API, which brings significant performance benefits, especially during application startup.

This release also simplifies code structure for beginners, enhances security and initialization processes, streamlines module usage, and boosts the performance of fundamental data types like Strings. Let’s dive into the key highlights.

Boosting Performance with Stable Values API

One of the core goals of JDK 25 is to tackle application startup performance. Often, setting up complex objects needed early on can slow things down. Traditionally, developers use final fields for performance (they’re initialized once and are very fast), but this requires setting them up immediately. Lazy initialization (setting something up only when it’s first needed) saves startup time but can involve complicated, less performant checks every time the value is accessed.

The new Stable Values API, introduced via JEP 502, offers a clever solution. It lets you define a value that can be set at most once. Think of it like a variable that starts empty but, the first time you ask for its value, it calculates it and then locks it in forever, making subsequent accesses just as fast as a final field. This initialization is handled safely, even across different parts of your program running at the same time (thread-safe).

This approach allows the Java Virtual Machine (JVM) to perform optimizations typically reserved for final fields, but with the flexibility of delayed setup.

The heart of this API is the StableValue class. You create one, and its value can only be set by calling the orElseSet() method. If the value hasn’t been set yet, the code you provide in orElseSet() runs, sets the value, and returns it. If it’s already set, the existing value is returned instantly.

Here’s a simple look at how it works:

class OrderController {
    private final StableValue<logger> logger = StableValue.of();

    Logger getLogger() {
        return logger.orElseSet(() -> Logger.create(OrderController.class));
    }
}</logger>

This pattern simplifies common code for getting a resource (like a logger) that you only need once, but not necessarily right when the object is created. It replaces more complex manual lazy initialization patterns with a clear, thread-safe, and optimized mechanism. The API also includes extensions for managing lists and suppliers of stable values.

Abstract gears representing optimization and efficiency in Java developmentAbstract gears representing optimization and efficiency in Java development

Making Java Easier and More Flexible

JDK 25 isn’t just about performance under the hood; it also introduces changes that simplify the coding experience, particularly for newcomers.

Simpler Starts: Instance Main Methods and Compact Source Files

Writing your first “Hello, World!” in Java traditionally requires understanding classes, static methods, and explicit imports. JEP 512 aims to lower this barrier with Compact Source Files and Instance Main Methods.

Now, you can write incredibly simple programs like this:

void main() {
    IO.println("Hello, World!");
}

The Java launcher is smarter and can look for an instance main method (void main()) if it doesn’t find the traditional static main method. This allows for much more concise single-file programs.

Accompanying this is the new java.lang.IO class, providing basic input/output operations without needing explicit import statements in these compact files, further simplifying things.

Flexible Constructor Bodies

Constructors, which set up new objects, have traditionally had a strict rule: the very first thing they must do is call another constructor (using super(...) or this(...)). JDK 25 introduces Flexible Constructor Bodies, allowing developers to include statements before this call.

This might sound small, but it significantly improves safety and initialization. It lets you perform checks or simple setups for fields before passing those values to another constructor, ensuring that the values used by super() or this() are already in a safe state.

Streamlined Module Imports

Java’s module system helps organize large applications, but importing packages from modules could still lead to long lists of import statements. JEP 511 introduces Module Import Declarations, allowing you to import all exported packages from a module with a single, clean declaration. This reduces clutter and makes managing dependencies in modular projects much simpler.

Faster String Operations

Strings are fundamental to almost every Java program, and JDK 25 brings a performance boost here too. The implementation of String::hashCode, which is used frequently (for instance, when using Strings as keys in maps), has been optimized. This change can lead to noticeable speed improvements in operations that rely heavily on string lookups, especially in applications using immutable maps.

Speaking of efficiency and security in applications, managing user authentication can be complex. Solutions like passwordless authentication (using passkeys, OTPs) offered by providers such as mojoauth can streamline user login processes for web and mobile applications, enhancing both security and user experience.

Developer working on a laptop, representing modern Java development with new JDK 25 featuresDeveloper working on a laptop, representing modern Java development with new JDK 25 features

Looking Ahead

As Java enters its fourth decade, it continues to evolve with features like the Stable Values API that offer real-world benefits in performance and developer productivity. JDK 25 also lays groundwork and refines ongoing projects such as the Foreign Function and Memory API and Structured Concurrency, promising even more powerful and easier ways to write complex applications in the future.

This latest release reinforces Java’s position as a robust and modern platform for enterprises, supported by a dynamic community and a vast ecosystem. For businesses focused on managing user identities and access efficiently, solutions like CIAM (Customer Identity and Access Management) platforms offer comprehensive tools.