How to Clone an Object in Java
- Use the Direct Method to Clone an Object in Java
-
Use the
clone()
Function to Clone an Object in Java - Use a Copy Constructor to Clone an Object in Java
Object cloning is a technique for making an exact duplicate of an object. It creates a new instance of the current object’s class and fills all of its fields with the exact contents of the current object’s fields.
In this tutorial, we will clone an object in Java.
Use the Direct Method to Clone an Object in Java
In this method, we will create a new object and assign some previously defined object as its value.
See the following example.
public class shallowcopy implements Cloneable {
int a, b;
public static void main(String args[]) throws CloneNotSupportedException {
shallowcopy d = new shallowcopy();
d.a = 10;
d.b = 12;
shallowcopy dtwo = d;
System.out.println("Original " + d.a);
System.out.println(d.b);
System.out.println("Shallow Copy " + dtwo.a);
System.out.println(dtwo.b);
d.a = 5;
System.out.println("Copy after modifying original");
System.out.println(dtwo.a);
}
}
Output:
Original 10
12
Shallow Copy 10
12
Copy after modifying original
5
In the above code, we altered the original and saw the changes reflected in the copy created. Such a clone is called a shallow copy.
Before returning a new instance of the same class, shallow copying produces a new instance of the same class and copies its fields. The contents of an object are transferred to a new object in this procedure. However, both the objects refer to the same values, so any modifications made to the linked objects will be reflected in others. The object class has a clone function as well as shallow copying capability. The object class has a clone function as well as shallow copying capability.
Use the clone()
Function to Clone an Object in Java
To clone an object, use the Object class’s clone()
method. It is the quickest way to duplicate an array.
The class whose object clone we wish to generate must implement the Cloneable interface. If the Cloneable interface is not implemented, the clone()
function throws a CloneNotSupportedException
.
The clone()
function eliminates the need for additional processing when making an exact replica of an object. If we do it using new
, it will take a long time to process, which is why we use object cloning instead. We will use it to create a deep copy.
When an item is duplicated together with the objects it references, it is called a deep copy. Any changes made in the copy do not reflect on the original.
For example,
import java.util.ArrayList;
public class deepcopy implements Cloneable {
int a, b;
public Object clone() throws CloneNotSupportedException {
return (deepcopy) super.clone();
}
public static void main(String args[]) throws CloneNotSupportedException {
deepcopy d = new deepcopy();
d.a = 10;
d.b = 12;
System.out.println("Original " + d.a);
System.out.println(d.b);
deepcopy dtwo = (deepcopy) d.clone();
System.out.println("Deep Copy " + dtwo.a);
System.out.println(dtwo.b);
d.a = 5;
System.out.println("Copy after modifying original");
System.out.println(dtwo.a);
}
}
Output:
Original 10
12
Deep Copy 10
12
Copy after modifying original
10
In the above example, changes made to the original are not reflected in the deep copy.
Use a Copy Constructor to Clone an Object in Java
A constructor initializes an object of some class. The copy constructor can create a duplicate object of some class.
See the code below to see how to initialize such a class.
class copycon {
public String cc;
public copycon(String s) {
this.cc = s;
}
public copycon(copycon different) {
this.cc = different.cc; // copy constructor
}
}
class Main {
public static void main(String args[]) {
copycon c1 = new copycon("abc");
copycon c2 = new copycon(c1);
System.out.println(c2.cc);
}
}
Output:
abc
This would not achieve deep cloning in Java.