Trust final fields in records

Chris Hegarty chris.hegarty at oracle.com
Tue Jun 16 11:08:42 UTC 2020


> On 16 Jun 2020, at 11:08, Kasper Nielsen <kasperni at gmail.com> wrote:
> 
>> One alternative might be an implicit static method parallel to the
>> constructor that
>> sets all field values, but does not execute any explicit constructor(s),
>> if present.
>> Sort of a 'raw' constructor.
> 
> Not having a 'raw' constructor that breaks encapsulation is a feature,
> not an oversight [1].

Exactly. The creation of deserialized record objects through an invocation of the canonical constructor is intentional. That is the whole point of the Serializable records work [*].

-Chris.

[*]  https://docs.oracle.com/en/java/javase/14/docs/specs/records-serialization.html

> /Kasper
> 
> [1] https://cr.openjdk.java.net/~briangoetz/amber/serialization.html



More information about the valhalla-dev mailing list