Quick revision
The short answer
The copy constructor shares name, because String is immutable; creates a new ArrayList for members and fills it with new Member(m) copies, because both the list and its elements can change; and creates a new HashMap from roles, because the map is mutable but its String keys and Role enum values are immutable and can be shared. If Team and Member were immutable, List.copyOf and Map.copyOf would give shallow but safe copies, since immutable elements never need copying.
What to remember
In this answer, we wrote a deep copy constructor for Team by asking, field by field, what can change. The String name is shared because it is immutable. The members list is a new ArrayList filled with new Member(m) copies, because both the list and the members are mutable. The roles map is a new HashMap whose String keys and Role values are shared because they are immutable. And if the classes were immutable, List.copyOf and Map.copyOf would give shallow copies that are just as safe.
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.
The copy constructor shares name, because String is immutable; creates a new ArrayList for members and fills it with new Member(m) copies, because both the list and its elements can change; and creates a new HashMap from roles, because the map is mutable but its String keys and Role enum values are immutable and can be shared. If Team and Member were immutable, List.copyOf and Map.copyOf would give shallow but safe copies, since immutable elements never need copying.
Why this question is asked
A deep copy is the kind of code that looks done after the first line and is only correct after thinking about every field. The question is not really about syntax; it is about which objects can change and who might change them. Interviewers ask it because the reasoning, field by field, is the same reasoning we need for defensive copies, snapshots and thread-safe handoffs. In this answer, we will recap shallow and deep copies, see a shallow copy go wrong, build the deep copy constructor one field at a time with the reason for each decision, and finish with the immutable version, which needs much less copying.
1A Quick Recap of Shallow and Deep Copies
Copying an object means creating a new object with the same state. A shallow copy copies each field's value; for a reference field, the value is a reference, so the copy points to the same object as the original. A deep copy also copies the objects that the fields refer to, as far down as needed, so that changes through the copy never affect the original.
"As far down as needed" is the key phrase. We only need to copy objects that can change. An immutable object, such as a String or an enum constant, can be shared safely between any number of owners. So writing a deep copy is really a matter of asking, for each field, whether anything reachable from it is mutable. Here are the classes we are copying:
2What Goes Wrong With a Shallow Copy
The tempting first version copies the references, and a slightly better one copies the list but not its elements. Both share state with the original.
Notice that version 2 fixes adding and removing members, because each team now has its own list, but the Member objects inside both lists are the same objects. Changing a member through the copy changes the original's member. That is exactly the bug a deep copy is meant to prevent, so we need to go one level further for members.
3The Deep Copy Constructor, Field by Field
Here is the correct constructor. Each field gets the treatment its type requires.
| Field | Mutable? | What the copy does |
|---|---|---|
| name: String | No | Shares the reference |
| members: List | Yes, the list | New ArrayList |
| each Member | Yes, has setters | new Member(m) via its copy constructor |
| roles: Map | Yes, the map | New HashMap |
| keys String, values Role | No: immutable String and enum | Share them |
name is a String, and Strings cannot change, so sharing the reference is a complete copy. members needs two levels: a new ArrayList so that adding or removing members in one team does not affect the other, and a new Member for each element, using Member's own copy constructor, because a Member has setters. roles also needs a new map, since the map is mutable, but its contents do not need copying. The keys are Strings and the values are Role enum constants, and both are immutable; in fact, an enum constant must never be copied, because each one is a unique instance by design.
Notice that the fields can all stay final. A copy constructor assigns them once, inside a constructor, which is exactly what final allows, and is one of the reasons copy constructors are preferred over clone().
4Copies Go as Deep as the Mutability
Member's copy constructor follows the same reasoning for its own fields. Its name and email are Strings, so it shares them. If Member had a mutable field, such as a List<String> of skills, its copy constructor would copy that list too. Each class is responsible for copying itself correctly, and Team simply calls new Member(m), so the knowledge stays where it belongs.
Two refinements are worth mentioning. A stream version, other.members.stream().map(Member::new).collect(Collectors.toCollection(ArrayList::new)), is equivalent and some find it clearer; we use toCollection rather than toList because toList returns an unmodifiable list, and Team expects to modify its members. And if other.members might contain null, the loop must handle it, though for a domain class it is usually better to forbid nulls when members are added.
5If the Classes Were Immutable
Much of this copying exists only because Member is mutable and Team's collections are modifiable. If Member were a record with no setters, and Team never modified its collections after construction, the copy becomes much simpler.
List.copyOf and Map.copyOf make shallow copies, but shallow is enough here, because nothing inside them can change: the collections are unmodifiable, and the elements are immutable records, Strings and enum constants. In fact, an immutable Team does not need a copy constructor at all, because sharing it is safe. These methods also reject null elements, and if the argument is already an unmodifiable list created by List.of or List.copyOf, they may return it unchanged rather than copying, which is safe for the same reason.
6Follow-up questions
What if Member had a reference back to its Team?
Then a naive deep copy recurses forever, or produces members that point to the old team. The copy must handle the cycle explicitly: create the new Team first, then copy each member with a reference to the new team, for example new Member(m, this), often keeping an identity map from old objects to new ones when the graph is more complex.
Is a defensive copy in a getter the same thing?
It is the same reasoning applied at a different moment. A getter that returns members directly lets callers modify Team's internal list. Returning List.copyOf(members) or Collections.unmodifiableList(members) prevents that; the first takes a snapshot, the second gives a read-only view that reflects later changes.
Why not use clone() for this?
Object.clone would make a shallow copy and then require assigning new collections to members and roles, which is impossible because they are final. A copy constructor can assign final fields and lets each class, such as Member, provide its own copy logic.
7Conclusion
In this answer, we wrote a deep copy constructor for Team by asking, field by field, what can change. The String name is shared because it is immutable. The members list is a new ArrayList filled with new Member(m) copies, because both the list and the members are mutable. The roles map is a new HashMap whose String keys and Role values are shared because they are immutable. And if the classes were immutable, List.copyOf and Map.copyOf would give shallow copies that are just as safe.
Open with
I go through the fields asking what can change. The name is a String, which is immutable, so the copy can just share the reference. The members list is mutable, so the copy needs its own ArrayList, otherwise adding a member to one team adds it to both.
Then, if they want more
But that alone is not enough, because the Member objects have setters: if both lists hold the same Member objects, changing an email through the copy changes the original. So I loop over other.members and add new Member(m) for each, using Member's own copy constructor, which copies whatever it needs to.
The roles map is also mutable, so it gets a new HashMap, but its contents can be shared: the keys are Strings and the values are Role enum constants, both immutable, and enum constants should never be copied anyway. A nice side effect is that all three fields can stay final, which clone could not handle.
If the design were immutable instead, with Member as a record and Team never modifying its collections, I would just use List.copyOf and Map.copyOf in the constructor: they are shallow copies, but shallow is safe when nothing inside can change, and then Team would not need a copy constructor at all.
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 deep copy constructor for this class and explain which fields need copying:": https://scrivio-interview-library.netlify.app/java/object-contracts/d5ec11d9f0/