Object cloning refers to creation of exact copy of an object. It creates a new instance of the class of current object and initializes all its fields with exactly the contents of the corresponding fields of this object.
Using Assignment Operator to create copy of reference variable
In Java, there is no operator to create copy of an object. Unlike C++, in Java, if we use assignment operator then it will create a copy of reference variable and not the object. This can be explained by taking an example. Following program demonstrates the same.
10 20 100 20 100 20
Creating a copy using clone() method
The class whose object’s copy is to be made must have a public clone method in it or in one of its parent class.
- Every class that implements clone() should call super.clone() to obtain the cloned object reference.
- The class must also implement java.lang.Cloneable interface whose object clone we want to create otherwise it will throw CloneNotSupportedException when clone method is called on that class’s object.
protected Object clone() throws CloneNotSupportedException
Usage of clone() method -Shallow Copy
10 20 300 40 100 20 300 40
In the above example, t1.clone returns the shallow copy of the object t1. To obtain a deep copy of the object certain modifications have to be made in clone method after obtaining the copy.
Deep Copy vs Shallow Copy
- Shallow copy is method of copying an object and is followed by default in cloning. In this method the fields of an old object X are copied to the new object Y. While copying the object type field the reference is copied to Y i.e object Y will point to same location as pointed out by X. If the field value is a primitive type it copies the value of the primitive type.
- Therefore, any changes made in referenced objects in object X or Y will be reflected in other object.
Shallow copies are cheap and simple to make. In above example, we created a shallow copy of object.
Usage of clone() method – Deep Copy
- If we want to create a deep copy of object X and place it in a new object Y then new copy of any referenced objects fields are created and these references are placed in object Y. This means any changes made in referenced object fields in object X or Y will be reflected only in that object and not in the other. In below example, we create a deep copy of object.
- A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.
10 20 30 40 100 20 300 0
In the above example, we can see that a new object for Test class has been assigned to copy object that will be returned in clone method.Due to this t3 will obtain a deep copy of the object t1. So any changes made in ‘c’ object fields by t3 ,will not be reflected in t1.
Advantages of clone method:
- If we use assignment operator to assign an object reference to another reference variable then it will point to same address location of the old object and no new copy of the object will be created. Due to this any changes in reference variable will be reflected in original object.
- If we use copy constructor, then we have to copy all of the data over explicitly i.e. we have to reassign all the fields of the class in constructor explicitly. But in clone method this work of creating a new copy is done by the method itself.So to avoid extra processing we use object cloning.
This article is contributed by Ankit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- LinkedList clone() Method in Java
- HashSet clone() Method in Java
- TreeSet clone() Method in Java
- HashMap clone() Method in Java
- Hashtable clone() Method in Java
- TreeMap clone() Method in Java
- EnumSet clone() Method in Java
- EnumMap clone() Method in Java
- IdentityHashMap clone() Method in Java
- GregorianCalendar clone() Method in Java
- Vector clone() Method in Java with Examples
- ConcurrentSkipListSet clone() method in Java
- CopyOnWriteArrayList clone() method in Java
- ArrayList clone() method in Java with Examples
- Stack clone() method in Java with Example
- BitSet clone() Method in Java with Examples
- TimeZone clone() Method in Java with Examples
- SimpleTimeZone clone() method in Java with Examples
- SimpleDateFormat clone() Method in Java with Examples
- Calendar clone() Method in Java with Examples
Improved By : Akanksha_Rai