Copy Constructor in Java with Examples
In Java, a copy constructor is a special constructor that is used to create a new object by copying the data of an existing object of the same class. Java does not provide a built-in copy constructor, but we can define one manually.
What is a Copy Constructor in Java?
A copy constructor is a constructor that accepts an object of the same class as its parameter and copies the values of its instance variables into the new object.
Syntax:
class ClassName {
ClassName(ClassName obj) {
// copy values
}
}
Why Do We Need a Copy Constructor?
- To create a duplicate object
- To avoid reference sharing
- To perform deep copy of objects
- To support immutability and data safety
Example 1: Simple Copy Constructor
class Student {
int id;
String name;
// Normal constructor
Student(int id, String name) {
this.id = id;
this.name = name;
}
// Copy constructor
Student(Student s) {
this.id = s.id;
this.name = s.name;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String[] args) {
Student s1 = new Student(101, "Chandan");
Student s2 = new Student(s1); // Copying object
s1.display();
s2.display();
}
}
Output:
101 Chandan
101 Chandan
Here, s2 is created by copying the values of s1,
but both are different objects in memory.
Proof That Objects Are Different
System.out.println(s1 == s2);
Output:
false
This proves that although data is copied, object references are different.
Shallow Copy Using Copy Constructor
When object references are copied instead of creating new objects, it is called a shallow copy.
class Address {
String city;
Address(String city) {
this.city = city;
}
}
class Employee {
int id;
Address address;
Employee(int id, Address address) {
this.id = id;
this.address = address;
}
// Shallow copy constructor
Employee(Employee e) {
this.id = e.id;
this.address = e.address;
}
}
In shallow copy, both objects share the same Address object.
Deep Copy Using Copy Constructor
In a deep copy, a new object is created for referenced objects as well.
class Employee {
int id;
Address address;
Employee(int id, Address address) {
this.id = id;
this.address = address;
}
// Deep copy constructor
Employee(Employee e) {
this.id = e.id;
this.address = new Address(e.address.city);
}
}
Now, changes made in one object will not affect the other.
Copy Constructor vs clone() Method
| Copy Constructor | clone() |
|---|---|
| Simple and readable | Complex and error-prone |
| No interface required | Requires Cloneable interface |
| Supports deep copy easily | Default is shallow copy |
| Recommended approach | Not recommended generally |
Key Points to Remember
- Java does not provide a copy constructor by default
- It must be created manually
- Used to copy data from one object to another
- Helps avoid unwanted reference sharing
- Preferred over
clone()
Conclusion
A copy constructor in Java is a powerful technique for creating object copies safely. It gives full control over how data is copied and is widely used in real-world applications for deep copying and immutability.
No comments:
Post a Comment