Java 14 records canonical constructor
Remi Forax
forax at univ-mlv.fr
Thu Jun 4 19:06:33 UTC 2020
----- Mail original -----
> De: "Christian Beikov" <christian.beikov at gmail.com>
> À: "Johannes Kuhn" <info at j-kuhn.de>
> Cc: "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Jeudi 4 Juin 2020 20:43:39
> Objet: Re: Java 14 records canonical constructor
> Am 04.06.2020 um 20:24 schrieb Johannes Kuhn:
>> They need to be mutable to do defensive copies, thereby enforcing deep
>> immutability. Consider this:
>>
>> public record UserRecord(String userName, List<String> mailAddresses) {
>> public UserRecord {
>> mailAddresses = List.copyOf(mailAddresses);
>> }
>> }
>>
>> Records would be a lot less useful if this is not possible.
>
> This could be done through a static factory method as well.
>
> public record UserRecord(String userName, List<String> mailAddresses) {
> public static UserRecord create(String userName, List<String>
> mailAddresses) {
> return new UserRecord(userName, List.copyOf(mailAddresses));
> }
> }
>
Nope, because serialization is based on the canonical constructor.
Rémi
More information about the amber-dev
mailing list