Untitled

Topic

Simple Cloning of objects with primitives

Shallow

Deep Object inside Object

Cloning with inheritance (Shallow)

Cloning with inheritance (Deep)

Serialization

Apache commons util

Copy constructor

Cloning objects with collections

Copy Constructor

Best practices

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.

package com.gs.corejava.collections;

//Java program to demonstrate that assignment 
//operator only creates a new reference to same 
//object. 

//A test class whose objects are cloned 
class Test {
	int x, y;

	Test() {
		x = 10;
		y = 20;
	}
}

// Driver Class
public class Main {
	public static void main(String[] args) {
		Test ob1 = new Test();

		System.out.println(ob1.x + " " + ob1.y);

		// Creating a new reference variable ob2
		// pointing to same address as ob1
		Test ob2 = ob1;

		// Any change made in ob2 will be reflected
		// in ob1
		ob2.x = 100;

		System.out.println(ob1.x + " " + ob1.y);
		System.out.println(ob2.x + " " + ob2.y);
	}
}

Output:

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.

  • Syntax:

Output:

Usage of clone() method -Shallow Copy

Output:

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.

Output:

Cloning with inheritance (Shallow) :

Example 1:

Example 2:

Output:

Last updated

Was this helpful?