About the possibility of using JEP468 to inspire optional fields in records for domain contracts modeling.

John Hendrikx john at int4.org
Tue Mar 11 20:37:24 UTC 2025


On 11/03/2025 15:48, david Grajales wrote:
> Why not use a class instead? Serialization. records' serialization
> uses the record constructor, which means it's safer since you can make
> format and safety validations in the data before the record is built.

While to deserialize a record, you are indeed forced to use the
constructor, nothing is stopping you from doing the exact same thing
with a class.  Serialization frameworks do not need access to setters or
fields.  A framework like Jackson can use the constructor perfectly
fine, and you can declare all fields private and final.  It is just a
matter of configuring it as such.

Such a constructor can then be outfitted with validations just like a
record constructor, and it will be enforced upon deserialization.

--John



More information about the amber-dev mailing list