What are Copy Constructors In Java? Explore Types, Examples & Use
A copy constructor is a constructor that creates a new object by copying the values of fields from an existing object of the same class. Unlike languages such as C++, Java does not provide a built-in copy constructor — you write one yourself.
Example
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
// Copy constructor
Student(Student other) {
this.name = other.name;
this.age = other.age;
}
}
Student s1 = new Student("Riya", 21);
Student s2 = new Student(s1); // copy of s1
Types of Copying
- Shallow Copy — copies primitive fields and reference values directly, so reference type fields still point to the same objects
- Deep Copy — creates new copies of any referenced objects as well, so the two objects are fully independent
Deep Copy Example
class Address {
String city;
Address(String city) { this.city = city; }
Address(Address other) { this.city = other.city; }
}
class Student {
String name;
Address address;
Student(Student other) {
this.name = other.name;
this.address = new Address(other.address); // deep copy
}
}
When to Use a Copy Constructor
- When you need an independent copy of an object without affecting the original
- When cloning objects that contain mutable fields, to avoid shared-state bugs
- As a cleaner alternative to Java's
Cloneableinterface andclone()method
clone().Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
What Happens in Memory During a Copy
When you call new Student(s1), Java allocates a brand-new object on the heap and then runs your copy constructor. Each line inside it decides what the new object receives:
- Primitive fields such as
intordoubleare copied by value, so changing one object never affects the other. - Immutable references such as
Stringcan be shared safely, because nobody can change the shared object. - Mutable references such as lists, arrays, dates from older APIs or your own classes must be copied explicitly if you want independence.
The constructor is just ordinary code, so you can also validate input, skip fields that should not be copied (like a generated ID) or reset counters.
Copying Collections Safely
The most common real-world bug is copying a list reference instead of the list itself. The JDK already uses copy constructors for this: new ArrayList<>(otherList) builds a new list with the same elements.
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class Team {
private final String name;
private final List<String> members;
Team(String name, List<String> members) {
this.name = name;
this.members = new ArrayList<>(members); // defensive copy
}
// Copy constructor reuses the main constructor
Team(Team other) {
this(Objects.requireNonNull(other).name, other.members);
}
void addMember(String person) { members.add(person); }
List<String> getMembers() { return List.copyOf(members); }
}
Team alpha = new Team("Alpha", List.of("Asha", "Ravi"));
Team beta = new Team(alpha);
beta.addMember("Neha");
System.out.println(alpha.getMembers()); // [Asha, Ravi]
System.out.println(beta.getMembers()); // [Asha, Ravi, Neha]
Notice that the list is new, but the String elements inside it are shared. That is fine because strings are immutable. If the list held mutable objects, you would copy each element too.
Copy Constructor or clone()?
| Point | Copy constructor | clone() |
|---|---|---|
| Extra interface needed | No | Must implement Cloneable |
| Checked exception | None | CloneNotSupportedException to handle |
| Works with final fields | Yes | Awkward for deep copies |
| Return type | Exact class, no cast | Often needs a cast |
| Polymorphic copying | No, the declared type decides | Yes, if overridden correctly |
The last row matters with inheritance. If a variable of type Employee actually holds a Manager, then new Employee(e) creates a plain Employee and the manager-specific fields are lost. In that case, add a copy() method that each subclass overrides and implements with its own copy constructor.
Mistakes to Avoid
- Assigning
this.list = other.listand calling it a copy. - Forgetting to handle a
nullargument, which causes a confusingNullPointerExceptionlater. - Deep copying objects that are immutable, which only wastes memory.
- Not updating the copy constructor after adding a new field to the class.
A simple unit test protects you here: create an object, copy it, change every mutable field on the copy, and assert that the original still holds its old values. If someone later adds a field and forgets the copy logic, the test fails straight away.
Takeaway: copy primitives and immutable values directly, create new instances of mutable fields, and test the copy by changing it and checking that the original stays the same.