This task summarizes the language changes introduced by JEP 401. All features are activated by the `--enable-preview` javac option.
See full JLS changes at: https://cr.openjdk.org/~dlsmith/jep401/latest/
### 'value' classes and interfaces
A class with the 'value' modifier, either concrete or abstract, is subject to the following rules:
- All instance fields are implicitly 'final'
- All fields must be initialized before the 'super' call (see also construction, below)
- No instance methods may be 'synchronized'
- Extends an abstract value class or 'Object'
- If not 'abstract', is implicitly 'final'
Abstract value classes may declare (final) instance fields.
A record class may be declared a 'value record' and is subject to these rules.
### Value class construction
As enabled by the Flexible Constructor Bodies JEP, any blank fields of a class may be assigned to before the 'super()' call.
In value classes, there are two changes in the timing of implicit constructor code:
- Instance field initializers are executed immediately upon entry to a non-'this()'-calling constructor.
- An implicit 'super()' call occurs at the *end* of a constructor that lacks an explicit constructor call.
Instance initializer blocks continue to run immediately after a 'super' call, wherever it occurs.
These changes imply that, in value classes, instance field initializers and "simple" constructor bodies occur in an early construction context and are not allowed to read 'this'.
### 'synchronized' statements
A compile-time error occurs if the type of the operand of a 'synchronized' statement is a concrete value class.
### Compilation
See JDK-8317278 for class file format.
The ACC_IDENTITY flag is unset on value classes, set on identity classes, and unset on interfaces.
The ACC_STRICT flag is set for all instance fields declared by a value class.
In each compiled class file, any concrete value class that appears in the descriptor of any declared or referenced field or method is named by the class file's `LoadableDescriptors` attribute.
All value classes and all classes or interfaces that refer to APIs with concrete value class types (i.e., that have a `LoadableDescriptors` attribute) are generated with the preview version number.
### Class file reading
When preview features are enabled, the following classes are considered to be value classes when loaded from class files:
- java.lang.Number
- java.lang.Record
- All 8 primitive wrapper classes
- All 4 java.util.Optional* classes
- 12 java.time classes: Duration, Instant, LocalTime, Year, YearMonth, MonthDay, Period, LocalDate, LocalDateTime, OffsetTime, OffsetDateTime, ZonedDateTime
- 5 java.time.chrono classes: ChronoLocalDateImpl, MinguoDate, HijrahDate, JapaneseDate, and ThaiBuddhistDate
### Compilation warnings
The `serial` category of the javac `-Xlint` feature adds a warning for a concrete Serializable class that meets all of the following conditions, so cannot be serialized or deserialized: 1) either is a value class, or is an identity class that extends a Serializable abstract value class other than Number, 2) doesn't declare/inherit a writeReplace method of the appropriate form for serialization to use, 3) isn't a record, and 4) isn't a primitive wrapper class
### Other language tools and APIs
- 'javadoc' output indicates when a class is a value class.
- 'javax.lang.model' is updated to support the 'value' modifier.
- 'java.lang.invoke.LambdaMetafactory' may experiment with value classes as lambda classes.
See full JLS changes at: https://cr.openjdk.org/~dlsmith/jep401/latest/
### 'value' classes and interfaces
A class with the 'value' modifier, either concrete or abstract, is subject to the following rules:
- All instance fields are implicitly 'final'
- All fields must be initialized before the 'super' call (see also construction, below)
- No instance methods may be 'synchronized'
- Extends an abstract value class or 'Object'
- If not 'abstract', is implicitly 'final'
Abstract value classes may declare (final) instance fields.
A record class may be declared a 'value record' and is subject to these rules.
### Value class construction
As enabled by the Flexible Constructor Bodies JEP, any blank fields of a class may be assigned to before the 'super()' call.
In value classes, there are two changes in the timing of implicit constructor code:
- Instance field initializers are executed immediately upon entry to a non-'this()'-calling constructor.
- An implicit 'super()' call occurs at the *end* of a constructor that lacks an explicit constructor call.
Instance initializer blocks continue to run immediately after a 'super' call, wherever it occurs.
These changes imply that, in value classes, instance field initializers and "simple" constructor bodies occur in an early construction context and are not allowed to read 'this'.
### 'synchronized' statements
A compile-time error occurs if the type of the operand of a 'synchronized' statement is a concrete value class.
### Compilation
See JDK-8317278 for class file format.
The ACC_IDENTITY flag is unset on value classes, set on identity classes, and unset on interfaces.
The ACC_STRICT flag is set for all instance fields declared by a value class.
In each compiled class file, any concrete value class that appears in the descriptor of any declared or referenced field or method is named by the class file's `LoadableDescriptors` attribute.
All value classes and all classes or interfaces that refer to APIs with concrete value class types (i.e., that have a `LoadableDescriptors` attribute) are generated with the preview version number.
### Class file reading
When preview features are enabled, the following classes are considered to be value classes when loaded from class files:
- java.lang.Number
- java.lang.Record
- All 8 primitive wrapper classes
- All 4 java.util.Optional* classes
- 12 java.time classes: Duration, Instant, LocalTime, Year, YearMonth, MonthDay, Period, LocalDate, LocalDateTime, OffsetTime, OffsetDateTime, ZonedDateTime
- 5 java.time.chrono classes: ChronoLocalDateImpl, MinguoDate, HijrahDate, JapaneseDate, and ThaiBuddhistDate
### Compilation warnings
The `serial` category of the javac `-Xlint` feature adds a warning for a concrete Serializable class that meets all of the following conditions, so cannot be serialized or deserialized: 1) either is a value class, or is an identity class that extends a Serializable abstract value class other than Number, 2) doesn't declare/inherit a writeReplace method of the appropriate form for serialization to use, 3) isn't a record, and 4) isn't a primitive wrapper class
### Other language tools and APIs
- 'javadoc' output indicates when a class is a value class.
- 'javax.lang.model' is updated to support the 'value' modifier.
- 'java.lang.invoke.LambdaMetafactory' may experiment with value classes as lambda classes.
- csr for
-
JDK-8339181 Java language implementation of value classes and objects
- Draft
- relates to
-
JDK-8347473 [lworld] LoadableDescriptors for dynamically generated classes
- Open
-
JDK-8326429 [lworld] Java language implementation of value classes and objects, phase 1
- Resolved