JDK-8339181 : Java language implementation of value classes and objects
  • Type: CSR
  • Component: tools
  • Sub-Component: javac
  • Priority: P4
  • Status: Proposed
  • Resolution: Unresolved
  • Fix Versions: tbd
  • Submitted: 2024-08-28
  • Updated: 2024-08-31
Related Reports
CSR :  
Relates :  
Relates :  
Description
Summary
-------

Enhance the Java language with value classes and objects.

Problem
-------

[The JEP](https://openjdk.org/jeps/401) provides a high-level discussion motivating the feature.

In `--enable-preview` mode, the Java language needs the following new capabilities:

- A way to express, and validate, that a given class (abstract or concrete) has no dependency on identity
- Constraints on instance creation of these classes, so that final fields can never be observed to mutate
- Identity-free semantics for operations on reference types, including the `==` and `!=` operators and the `synchronized` statement
- Automatically interpreting certain JDK classes to have no identity at compile time

Solution
--------

Our solution enhances the Java language as follows:

- Introduce the `value` modifier for classes (not interfaces or enums, but including records); a `value` class is either `abstract` or implicitly `final`, and may not extend an identity class (other than `Object`); instance fields are implicitly `final`, and instance methods may not be `synchronized`
- Require that the instance fields of a value class be assigned before the `super()` constructor call; where this call is implicit, place it at the *end*, not the *start*, of a value class constructor body
- Specify when two value class instances are "the same", and so will successfully pass a `==` test; specify that a `synchronized` statement will throw when operating on a value class instance, and reject programs with `synchronized` statements operating on final value class types
- Designate certain JDK classes as value classes when in `--enable-preview` mode

Any class files that _declare_ or _use_ value classes are marked as preview-versioned class files. (This includes all classes that use, e.g., `Integer` or `Optional`.)

`javac` also adds new checks to its `-Xlint:serial` warnings feature, identifying value classes that implement `Serializable` but that serialization cannot support (see also [JDK-8317279](https://bugs.openjdk.org/browse/JDK-8317279)).

The `javax.lang.model` API and `javadoc` tool include trivial enhancements to surface the `value` modifier.

A more comprehensive outline of the solution can be found in the [JDK-8317277](https://bugs.openjdk.org/browse/JDK-8317277) description.

Specification
-------------

JLS changes are included in the attached spec change document bundle. See the file `specs/value-objects-jls.html`.

Comments
JLS changes can also be reviewed at https://cr.openjdk.org/~dlsmith/jep401/jep401-20240624/specs/value-objects-jls.html
29-08-2024