Quick revision
The short answer
We write record Range(int lo, int hi) { Range { if (lo > hi) throw new IllegalArgumentException(lo + " > " + hi); } }. The compact canonical constructor has no parameter list and no field assignments: the compiler assigns the fields from the parameters at the end, after our checks, and we may reassign the parameters first to normalise them. A full canonical constructor declares the parameters itself and must assign every field explicitly.
What to remember
In this answer, we built a Range record that rejects lo greater than hi with a compact canonical constructor. We saw that the compact form has no parameter list, that the compiler assigns the fields from the parameters at the end, and that we can reassign the parameters first to normalise values. The full canonical constructor declares the parameters and must assign every field itself. Any other constructor must delegate with this(...), so all construction goes through one check.
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.
We write record Range(int lo, int hi) { Range { if (lo > hi) throw new IllegalArgumentException(lo + " > " + hi); } }. The compact canonical constructor has no parameter list and no field assignments: the compiler assigns the fields from the parameters at the end, after our checks, and we may reassign the parameters first to normalise them. A full canonical constructor declares the parameters itself and must assign every field explicitly.
Why this question is asked
Validation is the first thing we usually add to a record, because the generated constructor accepts any values, including a range whose start is after its end. Interviewers ask this question to check that we know the compact form, what the compiler fills in for us, and the rules that tie all of a record's constructors together. In this answer, we will see the broken record without validation, add a compact constructor, use it to normalise values, compare it with the full canonical form, and then look at how other constructors must delegate.
1The Problem: A Record Accepts Any Values
A record declaration such as record Range(int lo, int hi) {} gives us a canonical constructor, a constructor whose parameters exactly match the components, for free. That generated constructor simply copies its arguments into the fields. It has no idea that a range should not end before it starts.
Every piece of code that receives a Range now has to wonder whether it is valid. The fix is to make an invalid Range impossible to create, so the check lives in exactly one place.
2Adding a Compact Canonical Constructor
A compact canonical constructor is written with the record name and a body, without parentheses. Inside the body, the component names refer to the constructor's parameters, so lo and hi are the values the caller passed.
- Caller passes lo = 3, hi = 10arguments become the parameters
- Run the compact constructor bodyour checks and normalisation
- lo > hi?YesThrow IllegalArgumentExceptionNoContinue
- Compiler-added assignments
this.lo= lo;this.hi= hi - Object returned to callerRange[lo=3, hi=10]
5 steps in order. Play them, or step through them one at a time.
Our code runs first; the field assignments the compiler adds come last.
Notice what is missing: there is no this.lo = lo. The compiler adds the field assignments at the end of the compact constructor, after our code has run. So if our check throws, the fields are never assigned and no object escapes. In fact we are not allowed to assign the fields ourselves; writing this.lo = lo inside a compact constructor is a compile error, because the compiler owns that step.
3Normalising Values by Reassigning the Parameters
Because the assignments happen at the end, we can change the parameters before they are stored. This is the standard way to normalise input in a record: trim a string, copy a list, or reorder two values.
Whether we reject or swap depends on the domain. For a Range typed by a person, swapping might be friendly; for a Range coming from another system, rejecting is usually safer because a reversed range points to a bug upstream. Either way, the rule lives in one place and every Range in the program obeys it.
4The Full Canonical Constructor for Comparison
We can also write the canonical constructor in its full form, with the parameter list spelled out. Then it behaves like an ordinary constructor: nothing is assigned for us, and every field must be assigned exactly once, or the code does not compile.
Both forms are the canonical constructor; we may declare one or the other, not both. The parameter names and types must match the components, and the constructor must be at least as accessible as the record. The compact form is preferred for validation and normalisation because it removes the repetitive assignments and makes it impossible to forget one. The full form is useful when the assignment itself needs to be explicit, for example when we want to annotate the parameters differently, but that is rare.
5Other Constructors Must Delegate
A record can have additional constructors, but each of them must start by calling another constructor with this(...), and the chain must end at the canonical constructor. This rule guarantees that every Range, however it was created, passes through our validation.
So the compact constructor is the single gate for all construction, including deserialization, because Java serialization and Jackson both call the canonical constructor for records.
6Follow-up questions
Can a compact constructor be private?
No. The canonical constructor must be at least as accessible as the record itself, so a public record has a public canonical constructor. If we want to force callers through a factory, a record is not the best fit; a final class with a private constructor gives that control.
Where does the IllegalArgumentException message come from?
From our own code. Building it as lo + " > " + hi gives a message such as 10 > 3, which tells whoever reads the log exactly which values were wrong. Including the values is cheap and saves a debugging session later.
Can the compact constructor read the fields through this?
Not before they are assigned, and they are only assigned after the body. Inside the body we work with the parameters. Calling an accessor such as this.lo() there would read an unassigned field, and the compiler reports an error for direct field reads before assignment.
7Conclusion
In this answer, we built a Range record that rejects lo greater than hi with a compact canonical constructor. We saw that the compact form has no parameter list, that the compiler assigns the fields from the parameters at the end, and that we can reassign the parameters first to normalise values. The full canonical constructor declares the parameters and must assign every field itself. Any other constructor must delegate with this(...), so all construction goes through one check.
Open with
I would write record Range(int lo, int hi) with a compact canonical constructor: Range, open brace, if lo is greater than hi throw new IllegalArgumentException with lo, greater than, hi in the message, close brace.
Then, if they want more
The compact form has no parameter list. Inside it, lo and hi are the constructor parameters, and the compiler adds this.lo = lo and this.hi = hi at the end of the body, after my checks. So if the check throws, no object is ever created.
I am not allowed to assign the fields myself there; that is a compile error. What I can do is reassign the parameters before they are stored, which is the normal way to normalise, for example swapping lo and hi if the domain prefers that, or name = name.strip() in a string record.
The full canonical constructor is the same constructor written with the parameter list. Then nothing is automatic, and I must assign every field explicitly, or it does not compile. I can declare one form or the other, not both, and the compact form is usually better because I cannot forget an assignment.
Finally, any other constructor in a record, like Range(int point), must call this(point, point), and the chain always ends at the canonical constructor. Because Java serialization and Jackson also use the canonical constructor, every Range in the program passes through that one check.
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 "Write a record Range(int lo, int hi) that rejects lo > hi, using a compact canonical...": https://scrivio-interview-library.netlify.app/java/records/f0482554a7/