How to Use Copy Constructor in Java: Syntax and Example
In Java, a
copy constructor is a special constructor that creates a new object by copying fields from an existing object of the same class. You define it by passing an object of the class as a parameter and copying its values inside the constructor.Syntax
A copy constructor in Java looks like a normal constructor but takes an object of the same class as a parameter. Inside, it copies the values of the fields from the passed object to the new object.
- ClassName(ClassName obj): The constructor name matches the class name.
- obj: The object to copy from.
- Copy each field from
objtothisobject.
java
public class ClassName { int field1; String field2; // Copy constructor public ClassName(ClassName obj) { this.field1 = obj.field1; this.field2 = obj.field2; } }
Example
This example shows a Person class with a copy constructor that copies the name and age from another Person object. It demonstrates creating a new object by copying an existing one.
java
public class Person { String name; int age; // Normal constructor public Person(String name, int age) { this.name = name; this.age = age; } // Copy constructor public Person(Person other) { this.name = other.name; this.age = other.age; } public void display() { System.out.println("Name: " + name + ", Age: " + age); } public static void main(String[] args) { Person original = new Person("Alice", 30); Person copy = new Person(original); // Using copy constructor original.display(); copy.display(); } }
Output
Name: Alice, Age: 30
Name: Alice, Age: 30
Common Pitfalls
Common mistakes when using copy constructors include:
- Not copying all fields, which leads to incomplete copies.
- Copying references for mutable objects instead of creating new copies (shallow copy vs deep copy).
- Forgetting to define a copy constructor, causing errors when trying to copy objects.
Always decide if you need a shallow or deep copy depending on your fields.
java
public class Box { int size; int[] items; // Shallow copy constructor (wrong if items should be independent) public Box(Box other) { this.size = other.size; this.items = other.items; // Copies reference only } // Deep copy constructor (correct for independent copy) public Box(Box other, boolean deepCopy) { this.size = other.size; this.items = new int[other.items.length]; for (int i = 0; i < other.items.length; i++) { this.items[i] = other.items[i]; } } }
Quick Reference
- Copy constructor name = class name.
- Parameter is an object of the same class.
- Copy all fields carefully.
- Use deep copy for mutable objects.
- Helps create exact copies of objects easily.
Key Takeaways
A copy constructor creates a new object by copying fields from an existing object of the same class.
Always copy all fields and decide between shallow and deep copy based on field types.
The copy constructor has the same name as the class and takes an object of that class as a parameter.
Use copy constructors to simplify object duplication and avoid manual field copying.
Be careful with mutable objects to avoid shared references causing bugs.