Quick revision
The short answer
No. When a record is deserialized, Java reads the component values from the stream and then calls the canonical constructor with them, so the compact constructor's check runs and the crafted stream fails with an exception. An ordinary serializable final class is different: deserialization does not run its constructor, so a stream with lo = 5 and hi = 1 produces exactly that object unless the class adds a readObject that validates and throws InvalidObjectException. This is one strong reason to prefer records for serializable value data.
What to remember
In this answer, we showed that a crafted stream cannot create a Range record with lo greater than hi, because record deserialization reads the component values and calls the canonical constructor, whose check throws. The same stream creates an invalid OldRange, because ordinary serializable classes are rebuilt without running their constructor. We fixed OldRange with a readObject that validates and throws InvalidObjectException, and saw why records, with a single path into the object, are the better choice for serializable value data.
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.
No. When a record is deserialized, Java reads the component values from the stream and then calls the canonical constructor with them, so the compact constructor's check runs and the crafted stream fails with an exception. An ordinary serializable final class is different: deserialization does not run its constructor, so a stream with lo = 5 and hi = 1 produces exactly that object unless the class adds a readObject that validates and throws InvalidObjectException. This is one strong reason to prefer records for serializable value data.
Why this question is asked
Validation in a constructor feels like a guarantee: if the constructor refuses bad values, no bad object can exist. Java serialization breaks that guarantee for ordinary classes, and records were designed to restore it. Interviewers ask this coding question to see whether we know how each kind of class is rebuilt from bytes, and whether we can show it rather than just say it. In this answer, we will recap the two classes, craft a bad stream by editing real serialized bytes, run it against the ordinary class and the record, fix the ordinary class with readObject, and see why records are the better default for value data.
1The Two Classes We Compare
Range is a record with a compact canonical constructor: the block Range { ... } runs before the fields are assigned, with the parameters lo and hi in scope, and throws if they are in the wrong order. OldRange is a normal final class with the same check in its constructor. Both are Serializable.
Through normal code, both are equally safe. The question is what happens when the object comes from bytes instead of from new.
2Crafting a Bad Stream
An attacker does not need special tools to craft a stream; they can take valid bytes and edit them. For these classes it is easy. The serialized form ends with the field values, and Java writes primitive fields sorted by name, so hi comes before lo. Swapping the last two ints of a valid Range(1, 5) stream gives a stream that says lo = 5 and hi = 1.
Notice that we never call a constructor with bad values. We only produce bytes, which is exactly the position an attacker is in when our service reads a stream from the network, a cache or a file.
3The Ordinary Class Accepts the Bad Stream
Now we feed the edited stream to OldRange.
6 messages in order. Play them, or step through them one at a time.
The record's check runs; the ordinary class's check is skipped.
The reason is how ordinary classes are deserialized. ObjectInputStream allocates an OldRange without calling its constructor; it only runs the no-argument constructor of the first non-serializable superclass, Object. Then it sets lo and hi directly from the stream, even though they are final. The check in OldRange's constructor never runs, so the object holds values no constructor would have allowed. Any code that later relies on lo <= hi, such as computing hi - lo as a length, now misbehaves.
4The Record Rejects It
Now the same trick against the record.
Records are deserialized differently by design. The stream holds only the component values. ObjectInputStream reads them first, matching them to components by name, and then creates the record by calling its canonical constructor with those values. The compact constructor runs, the check fails, and readObject throws instead of returning an object. Java reports the failure as an InvalidObjectException with the constructor's exception as its cause; check your version for the exact wrapping. Either way, no Range with lo > hi can ever exist.
5Fixing the Ordinary Class With readObject
If OldRange must stay a normal class, it needs to repeat its validation on the deserialization path. A private readObject method is called instead of default reading; we let defaultReadObject fill the fields and then check them.
This works, but the validation now lives in two places, and the two must be kept in sync forever. If someone later adds a field with its own rule, such as a step that must be positive, and only updates the constructor, the hole opens again. For classes with mutable reference fields, readObject must also make defensive copies, because the stream can share references to those objects with an attacker's other objects. Records avoid all of this, because there is only one path into the object.
6Follow-up questions
Why does the stream list hi before lo?
Java writes a class's serializable fields in a fixed order: primitive fields first, sorted by name, then object fields sorted by name. hi sorts before lo, so its four bytes come first. Records follow the same field order in the stream, which is why the same swap works for both classes.
Could an attacker use readResolve or other tricks against the record?
Records still honour writeReplace and readResolve if the record declares them, but the record itself always passes through its canonical constructor first, so its own invariants hold. Methods such as readObject and serialPersistentFields are ignored for records, so there is no second path for an attacker to use.
Should we validate in readObject or readResolve?
readObject with InvalidObjectException is the standard place for ordinary classes, because it runs as soon as the fields are read. An alternative is readResolve returning new OldRange(lo, hi), which reuses the constructor's check, or the serialization proxy pattern, which routes all deserialization through a constructor in the same way records do.
7Conclusion
In this answer, we showed that a crafted stream cannot create a Range record with lo greater than hi, because record deserialization reads the component values and calls the canonical constructor, whose check throws. The same stream creates an invalid OldRange, because ordinary serializable classes are rebuilt without running their constructor. We fixed OldRange with a readObject that validates and throws InvalidObjectException, and saw why records, with a single path into the object, are the better choice for serializable value data.
Open with
No, it cannot, and the difference comes from how each kind of class is rebuilt. For a record, the stream contains only the component values. ObjectInputStream reads them, matches them to the components by name, and then calls the canonical constructor with them, so the compact constructor's check runs, throws, and readObject fails, as an InvalidObjectException wrapping the IllegalArgumentException.
Then, if they want more
No Range with lo greater than hi can exist. For an ordinary final class with the same check in its constructor, deserialization never calls that constructor. It allocates the object, runs only Object's no-arg constructor, and sets lo and hi directly from the stream, even though they are final.
So if I take valid bytes for OldRange of 1 and 5 and swap the last two ints, which are hi and lo because primitive fields are written sorted by name, I get an OldRange with lo 5 and hi 1.
To fix the ordinary class, I add a private readObject that calls defaultReadObject and then checks lo against hi and throws InvalidObjectException, plus defensive copies if it had mutable reference fields. The problem is that the rule now lives in two places that must stay in sync.
That is one of the main reasons I prefer records for serializable value data: there is only one way in, the canonical constructor, so the invariants always hold.
About this answer
- Last updated
- 12 November 2026
- 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 "Given record Range(int lo, int hi) implements Serializable { Range { if (lo > hi) throw...": https://scrivio-interview-library.netlify.app/java/serialization/19f9eb9abb/