Make exact copy or Clone of a Java Bean Object.

Posted By : Lovish Pahwa | 27-Jun-2017

Cloning of object refers to make a copy of the object. In JAVA when we use JavaBeans for traveling data from layer to layer than some time we need a  copy/clone of a JavaBean object and there are many libraries to implement cloning.

Java OBJECT class is having a method i.e. clone() and as we all know that in Java every class internally extends Object class so in every class we can use this clone method to make a clone of its object.

To use this clone method for making a clone of any bean object than that bean class must implements an interface "Cloneable" otherwise clone method will not work as expected.

Now there are two types of cloning can happen and they are following:-

  1. Shallow clone/copy
  2. Deep clone/copy

Shallow clone:-

In Shallow cloning, an exact copy is made but without using new memory space, means a new reference is made but having the old instance in it.

Requirements:-

For shallow cloning we just have to do following steps:-

  1. Implements "Cloneable" interface in the bean class.
  2. Overriding clone method for default behavior.
  3. Use clone method with the object to make the clone.

Deep clone:-

In Deep cloning exact copy is made but with new memory instance, means a new reference is created with new memory instance but same properties.

Requirements:-

For Deep cloning we just have to do following steps:-

  1. Implements "Cloneable" interface in the bean class.
  2. Override clone method for deep copy behavior.
  3. Use clone method with an object to make its clone.

 

Cloning Example:-

class Student implements Cloneable
{

int id;
String name;
String address;

public Student(int id, String name, String address)
{
  this.id = id;
  this.name = name;
  this.address = address;
}

//Default version of clone() method. It creates shallow copy of an object.
protected Object clone() throws CloneNotSupportedException // Use this for Shallow Copy.
{
   return super.clone(); //Here same instance is returned to new reference.
}

//Modified version of clone() method. It creates Deep copy of an object.
protected Object clone() throws CloneNotSupportedException // Use this for Deep copy.
{
Student student = (Student) super.clone(); //Here new instance is created and returned to new reference.
return student;
}
}

public class JavaClonning
{
    public static void main(String[] args)
    {
Student studentOne = new Student ( 1512089, "lvsh", "rohtak, haryana" ); 
Student studentTwo = null;

        try
        {
//Creating a clone of studentOne and assigning it to studentTwo
studentTwo = studentOne.clone();

        }
        catch (CloneNotSupportedException e)
        {
            e.printStackTrace();
        }

    }
}
Thanks

About Author

Author Image
Lovish Pahwa

Lovish is an experienced Manager with strong knowledge of Spring Framework, Play Framework, Java, Javascript, JQuery, AngularJs, and SQL. He is a great problem solver and always ready for new challenges.

Request for Proposal

Name is required

Comment is required

Sending message..