Quick revision
The short answer
Make the class final and its fields private final, replace the mutable java.util.Date with an immutable java.time type such as Instant or LocalDateTime, copy the guest list in the constructor with List.copyOf, which returns an unmodifiable list and rejects nulls, and return that unmodifiable list from the getter so callers cannot change the state. A record with a compact constructor that copies the list gives the same result in a few lines.
What to remember
In this answer, we made Booking immutable step by step. We made the class final and its fields private final, replaced the mutable Date with an immutable java.time type such as Instant, copied the guest list in the constructor with List.copyOf so the caller's list cannot reach inside, and returned that unmodifiable list from the getter. We also saw that the same result as a record needs a compact constructor that copies the list, because records make fields final but do not copy mutable arguments.
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.
Make the class final and its fields private final, replace the mutable java.util.Date with an immutable java.time type such as Instant or LocalDateTime, copy the guest list in the constructor with List.copyOf, which returns an unmodifiable list and rejects nulls, and return that unmodifiable list from the getter so callers cannot change the state. A record with a compact constructor that copies the list gives the same result in a few lines.
Why this question is asked
Immutable objects cannot change after they are created, which makes them safe to share between threads, safe to use as map keys and cache values, and easy to reason about. The Booking class in the question looks harmless, with private fields and only getters, but it can be changed from outside in at least three ways. Interviewers ask this because each fix teaches a separate rule, and many candidates stop after adding final. In this answer, we will show how the original class can be mutated, fix each problem in turn with the reason for the change, and then write the same thing as a record.
1How the Original Class Can Be Changed
Here is the class from the question, and three pieces of code that change a booking without calling any setter.
Private fields stop other classes from reassigning the fields, but they do not stop anyone from changing the objects the fields point to. The constructor stores the caller's list and date by reference, and the getters give those same references back out. Anyone holding them can change the booking. There is a fourth weakness too: the class is not final, so a subclass could add mutable state or override getters to return different values.
2A Final Class With Private Final Fields
Declaring the class final prevents subclasses, so no subclass can break the guarantee by overriding a getter or adding a setter. Declaring the fields final means they are assigned exactly once, in the constructor, and the compiler rejects any later assignment. Final fields also carry a guarantee from the Java Memory Model: any thread that sees the constructed object sees those fields correctly initialised, without synchronisation. But final only freezes the reference. A final List can still have elements added, and a final Date can still have setTime called, so the next two steps are needed.
3Replace Date With a java.time Type
java.util.Date is mutable through setTime and several deprecated setters. The classes of java.time, added in Java 8, are immutable: every method that seems to change a value returns a new object. Choosing the right one depends on what start means. Instant is a point on the global timeline, which is what Date really represented. LocalDateTime is a date and time without a time zone, such as 14:00 on 3 March wherever the hotel is; if the hotel's zone matters, ZonedDateTime or a LocalDateTime plus a ZoneId is better. For a hotel booking that starts on a day, LocalDate may be the honest type.
If an API forces us to keep Date, the fallback is defensive copying in both directions: this.start = new Date(start.getTime()) in the constructor and return new Date(start.getTime()) in the getter. That works but must be remembered everywhere; switching to Instant removes the problem.
4Copy the List In, Hand Out an Unmodifiable One
List.copyOf, added in Java 10, returns an unmodifiable list with the same elements. It is a copy, so later changes to the caller's list do not reach the booking, and it is unmodifiable, so the getter can return it directly: any attempt to add, remove or clear throws UnsupportedOperationException. It also rejects null elements and a null list with a NullPointerException, which is usually what we want for a guest list. If the argument is already a list returned by List.copyOf or List.of, it may return the same instance without copying, which is safe because that list cannot change.
| Change | Hole it closes |
|---|---|
| final class | Subclasses overriding getters or adding state |
| private final fields | Reassignment after construction; safe publication |
| Date -> Instant | b.getStart().setTime(0) changing the booking |
List.copyOf in the constructor | Caller changing the list it passed in |
| Return the unmodifiable list | b.getGuests().clear() changing the booking |
Notice why the copy must happen in the constructor and not only in the getter. Collections.unmodifiableList(guests) in the getter would stop callers from changing the list through the getter, but the caller who passed the list in could still change it, and the booking would see the change, because an unmodifiable view is a window onto the original, not a copy. The elements themselves are Strings, which are immutable, so a shallow copy is enough; a list of mutable Guest objects would need those to be immutable too.
5The Same Result as a Record
A record is a class whose purpose is to carry data: it is final, its fields are private and final, and it gets a constructor, accessors, equals, hashCode and toString automatically. A compact constructor, a constructor without a parameter list, runs before the fields are assigned and can replace the parameters with validated or copied values.
The record gives the same guarantees as our hand-written class in a few lines, plus value-based equals and hashCode, which an immutable value type usually wants anyway. The one thing a record does not do for us is the defensive copy: without the compact constructor, the record would store the caller's mutable list just like the original class, so the List.copyOf line is essential.
6Follow-up questions
Why not use Collections.unmodifiableList in the constructor?
Collections.unmodifiableList(guests) wraps the caller's list without copying it, so the caller can still change the underlying list and the booking would see it. List.copyOf, or new ArrayList<>(guests) wrapped in an unmodifiable view, takes a real copy. List.copyOf is the simplest because it copies and makes the result unmodifiable in one call.
Does immutability make a Booking thread-safe?
Yes, once it is properly constructed, meaning the constructor does not leak this. All fields are final and nothing reachable from them can change, so any number of threads can read a Booking without locks, and the Java Memory Model guarantees they see the fully initialised state even if the reference was shared without synchronisation.
How do we change a booking then?
We create a new one. A method such as withGuests(List<String> newGuests) returns new Booking(start, newGuests), leaving the original untouched. Code that holds the old booking keeps a consistent value, which is exactly the property that makes immutable objects easy to share.
7Conclusion
In this answer, we made Booking immutable step by step. We made the class final and its fields private final, replaced the mutable Date with an immutable java.time type such as Instant, copied the guest list in the constructor with List.copyOf so the caller's list cannot reach inside, and returned that unmodifiable list from the getter. We also saw that the same result as a record needs a compact constructor that copies the list, because records make fields final but do not copy mutable arguments.
Open with
The class looks safe because the fields are private, but it can be changed from outside in three ways. The constructor keeps the caller's list, so the caller can add guests later.
Then, if they want more
The getter returns the internal list, so anyone can clear it. And Date is mutable, so getStart().setTime(0) moves the booking to 1970. So first I make the class final, so a subclass cannot override getters or add state, and the fields private final, so they are assigned once and also get the memory model's safe publication guarantee.
But final only freezes the reference, so I replace Date with a java.time type, Instant if it is a point in time, or LocalDateTime or LocalDate depending on what start means, because those are immutable.
For the list, I copy it in the constructor with List.copyOf, which makes an unmodifiable copy and rejects nulls, so later changes to the caller's list do not reach the booking. Then the getter can return that list directly, because any attempt to modify it throws UnsupportedOperationException.
A plain unmodifiable view in the getter would not be enough, since the caller's original list could still change. In modern Java I would write it as a record Booking(Instant start, List<String> guests) with a compact constructor that does guests = List.copyOf(guests), which gives the same guarantees plus equals, hashCode and toString.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 10, Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "Make this class immutable and explain each change:": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/4ac6d39abe/