-
CSR
-
Resolution: Unresolved
-
P4
-
None
-
source, behavioral
-
high
-
All instances of value classes, including classes like Integer, Optional, and LocalDate, behave differently under `==`, `!=`, and `synchronized`; existing `synchronized` statements may not compile
-
Java API, Language construct, add/remove/modify command line option
-
SE
Summary
Enhance the Java language with value classes and objects.
Problem
The JEP 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 thesynchronized
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); avalue
class is eitherabstract
or implicitlyfinal
, and may not extend an identity class (other thanObject
); instance fields are implicitlyfinal
, and instance methods may not besynchronized
- 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 asynchronized
statement will throw when operating on a value class instance, and reject programs withsynchronized
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).
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 description.
Specification
JLS changes are included in the attached spec change document bundle. See the file specs/value-objects-jls.html
.
- csr of
-
JDK-8317277 Java language implementation of value classes and objects
- In Progress
- relates to
-
JDK-8339130 JVM implementation of value classes and objects
- Draft
-
JDK-8339199 Standard library implementation of value classes and objects
- Draft