Quick revision
The short answer
A record is serialized as its component values only, and it is always deserialized by calling its canonical constructor with those values, so any validation in the constructor runs. Records ignore writeObject, readObject, readObjectNoData and serialPersistentFields, while writeReplace and readResolve still work. Because there is only one way to create a record, through its constructor, a crafted stream cannot produce a record that breaks its invariants, which is why record serialization is considered safer.
What to remember
In this answer, we saw that records are serialized as their component values and always deserialized through the canonical constructor, so validation in the constructor runs for every instance. writeObject, readObject, readObjectNoData and serialPersistentFields are ignored for records, while writeReplace and readResolve are still honoured. Components are matched by name and the serialVersionUID check is waived, so simple changes are tolerated. Because invariants are checked through the constructor, crafted streams cannot create invalid records, although filters and explicit formats are still needed for the rest of the stream.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A record is serialized as its component values only, and it is always deserialized by calling its canonical constructor with those values, so any validation in the constructor runs. Records ignore writeObject, readObject, readObjectNoData and serialPersistentFields, while writeReplace and readResolve still work. Because there is only one way to create a record, through its constructor, a crafted stream cannot produce a record that breaks its invariants, which is why record serialization is considered safer.
Why this question is asked
Records arrived in Java 16 as a compact way to declare data carriers, and their serialization was redesigned on purpose. The Java team used them to fix the main weakness of classic serialization: objects created without their constructors. Interviewers ask this question to check that we know records are not just shorter syntax, and that we understand what changed and why it matters for security. In this answer, we will recap how ordinary classes are serialized, look at what records do differently on writing and reading, see which customisation methods still apply, and discuss version changes and the limits of the safety.
1A Quick Recap of Ordinary Serialization
For an ordinary Serializable class, ObjectOutputStream writes a class descriptor and then every non-transient field, found by reflection. On reading, ObjectInputStream allocates the object without calling the class's constructor, sets the fields directly from the stream, even final ones, and then calls any readObject method the class defines. The class can also take over with writeObject and readObject, choose fields with serialPersistentFields, or replace the object with writeReplace and readResolve.
The weak point is the middle step. Because the constructor is skipped, every invariant the constructor checks can be broken by a crafted stream, and each class must repeat its checks in readObject to be safe. Most classes never do.
2What Records Do Differently
A record's state is fully described by its components, the values in its header such as record Money(long cents, String currency). Serialization uses exactly that. Writing a record writes its component values, nothing else. Reading a record reads the component values from the stream, matches them to the components by name, and then invokes the canonical constructor, the one with all components as parameters, passing those values. The object is created by the constructor, as if our own code had called new.
| Step or feature | Ordinary class | Record |
|---|---|---|
| What is written | all non-transient fields | component values only |
| How the object is created | allocated, constructor skipped | canonical constructor called |
| Constructor validation on read | skipped | always runs |
writeObject / readObject | honoured | ignored |
readObjectNoData, serialPersistentFields | honoured | ignored |
writeReplace / readResolve | honoured | honoured |
serialVersionUID mismatch | InvalidClassException | check waived; default is 0L |
Notice that the validation we wrote once, in the compact constructor, now protects both normal construction and deserialization. There is no second path to forget about.
3Which Customisation Methods Still Apply
Since a record's serialized form is fixed to its components, the methods that change how fields are written and read make no sense, and the platform ignores them. If a record declares writeObject, readObject, readObjectNoData or serialPersistentFields, they are simply not called. This is deliberate: those methods are exactly the hooks that let classic serialization create objects outside the constructor.
writeReplace and readResolve are still honoured, because they replace whole objects rather than changing how fields are read. A record can use writeReplace to write a different object, and readResolve to return a canonical instance, for example a cached constant, after the record has been constructed. The record itself is still created through its constructor first, so its invariants hold either way.
4How Records Handle Version Changes
Because components are matched by name, records cope with simple changes. If the stream has a component the record no longer declares, its value is ignored. If the record has a new component that the stream lacks, the constructor receives the default value for its type, 0, false or null. The constructor then runs as usual, so if a new component must not be null, the constructor can supply a default or reject old data clearly.
Records also relax the serialVersionUID rule. A record's serialVersionUID is 0L unless it declares one, and the requirement that the stream's value matches the local value is waived for records, so adding a method no longer breaks old data the way it does for ordinary classes. Renaming a component, however, behaves like removing one and adding another, so the value is lost.
5Why This Is Safer, and Where the Limits Are
The safety comes from one property: every record instance, however it was made, passed through its canonical constructor. A crafted stream can supply any component values it likes, but it cannot produce an object that the constructor would refuse. Records also cannot have hidden state beyond their components, so there is nothing else for a stream to set.
But records do not make Java serialization safe in general. The stream still names classes, so a stream read with ObjectInputStream can still contain other, ordinary classes with gadget chains, and a record's component of type Object can hold anything. Validation also only checks what the constructor checks: if a component is a mutable list, the constructor should copy it, for example with List.copyOf, or the caller could still change it later. So records are the best choice for serializable value data, used together with deserialization filters, and explicit formats such as JSON or Protobuf remain the right choice across trust boundaries.
6Follow-up questions
Can a record declare serialVersionUID?
Yes, and it is allowed, but it has little effect: the matching requirement is waived for records, so a different value in the stream does not cause InvalidClassException. Without a declaration, a record's serialVersionUID is 0L.
Do records need to implement Serializable to be serialized?
Yes. Records are not serializable by default; they must implement Serializable like any class. All component types must also be serializable, or writing fails with NotSerializableException.
Do JSON libraries use the canonical constructor too?
Jackson has supported records since version 2.12 and creates them through the canonical constructor, so compact-constructor validation runs there as well. Check your library version, because older versions needed extra annotations.
7Conclusion
In this answer, we saw that records are serialized as their component values and always deserialized through the canonical constructor, so validation in the constructor runs for every instance. writeObject, readObject, readObjectNoData and serialPersistentFields are ignored for records, while writeReplace and readResolve are still honoured. Components are matched by name and the serialVersionUID check is waived, so simple changes are tolerated. Because invariants are checked through the constructor, crafted streams cannot create invalid records, although filters and explicit formats are still needed for the rest of the stream.
Open with
Records are serialized very differently from ordinary classes. When writing, only the component values are written, the values from the record header. When reading, ObjectInputStream reads those values, matches them to the components by name, and then calls the canonical constructor with them.
Then, if they want more
That is the key difference: for an ordinary class, deserialization allocates the object without calling its constructor and sets fields directly, even final ones, so any validation in the constructor is skipped unless the class repeats it in readObject.
For a record, the constructor always runs, so if my compact constructor rejects a negative amount, a crafted stream with a negative amount fails with InvalidObjectException instead of producing a bad object.
To keep that guarantee, the platform ignores writeObject, readObject, readObjectNoData and serialPersistentFields on records, because those are exactly the hooks that let classic serialization bypass constructors. writeReplace and readResolve are still honoured, since they replace whole objects after construction.
Records also handle simple version changes: missing components get default values, extra ones in the stream are ignored, and the serialVersionUID check is waived. So it is considered safer because every record instance passes through one constructor. I would still add a deserialization filter, since the stream can name other classes, and use JSON or Protobuf across trust boundaries.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does serialization of records differ from ordinary classes, and why is it considered...": https://scrivio-interview-library.netlify.app/java/serialization/77e0fb2407/