Select the directory option from the above "Directory" header!

Menu
OpenJDK plan would add computed constants to Java

OpenJDK plan would add computed constants to Java

Computed constants offer the performance and safety benefits of final fields, but greater flexibility in the timing of initialization, the proposal states.

Credit: Photo 206742130 | Coding Language © Wrightstudio | Dreamstime.com

An OpenJDK proposal would introduce computed constants to Java. Immutable value holders that are initialised at most once, computed constants offer the performance and safety benefits of final fields while offering more flexibility as to the timing of initialisation.

The proposal is currently in a draft status and eyed for standard Java. Driving the effort is the notion that developers should “prefer immutability,” because immutability confers a number of advantages. Immutable objects can only be in one state, which is carefully controlled by its constructor. Immutable objects can be freely shared with untrusted code. And immutability enables all manner of runtime optimizations.

Java’s main tool for managing immutability is final fields, but final fields have restrictions. As a result, developers must choose between the benefits of finality and flexibility in timing of initialisation. Developers have devised strategies to ameliorate the imbalance, but none are ideal.

The computed constants API defines classes and an interface so client code in applications and libraries can perform tasks including defining and using computed constant objects and constant collections. The API resides in the java.lang package of the java.base module.

Goals of the plan include:

  • Decoupling initialisation of computed constants from the initialisation of their containing object or class.
  • Providing an intuitive API for computed constants and collections.
  • Enabling constant folding optimisations for computed constants and supporting dataflow dependencies between these constants.
  • Reducing the amount of static initialiser code and/or field initialisation to be executed.
  • Allowing disentanglement of the “soup” of clinit dependencies by applying the above.
  • Upholding consistency and integrity, even in a multi-threaded environment.

Non-goals include providing additional language support for expressing constant computation and preventing or deprecating existing idioms for expressing lazy initialisation.

With the feature set for the next version of standard Java, Java Development Kit 21, now frozen, with the release due in September, the earliest possible version of standard Java that could include computed constants would be JDK 22, due in March 2024.


Follow Us

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Show Comments