Page 1 of 1

Confused by a Question on Record Serialization

Posted: Wed Oct 01, 2025 5:14 am
by lucasdowman
Hey all,
While practicing with Enthuware, I stumbled on a mock question about serialization of record classes in Java, and it threw me off completely. I thought records, by default, handle serialization just like normal POJOs since they’re implicitly Serializable when their fields are, but the explanation hinted at some subtle differences when custom constructors are involved.
Now I’m second-guessing myself: if I add validation logic in a compact constructor of a record Pips NYT, does it affect how the record behaves when deserialized? And if so, how do we predict that in an exam setting?
Has anyone else encountered this type of question, and how do you typically approach it during the test without overthinking?

Re: Confused by a Question on Record Serialization

Posted: Wed Oct 01, 2025 5:38 am
by admin
There is no need to overthink. Just go through this info:
records_serialization.png
records_serialization.png (82.43 KiB) Viewed 52 times
Taken from Hanumant Deshmukh's OCP Java 17/21 Fundamentals.

So basically, the canonical constructor will be called during deserialization with the values stored in the stream. The end result depends on what the constructor does.