Make exact copy or Clone of a Java Bean Object.
Posted By : Lovish Pahwa | 27-Jun-2017
Cloning of
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:-
- Shallow clone/copy
- 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:-
- Implements "Cloneable" interface in the bean class.
- Overriding clone method for default behavior.
- 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:-
- Implements "Cloneable" interface in the bean class.
- Override clone method for deep copy behavior.
- 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(); } } }
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.