In this post, we shall see how to make a copy of an object in Java. In our applications, JavaBeans
play a very important role. However sometimes we simply need to make a
copy of our JavaBeans so as to make changes to the copy and keep the
original object intact.
There are two ways in which this can be achieved in Java, depending upon the level of access you have to your beans.
- Using Object.clone()
- Using BeanUtils
Copying using Object.clone()
This method can be used when you have
access to the source code of your bean classes. This method requires
your JavaBeans to implement the cloneable interface. The Cloneable
interface is a marker interface that indicates that the object allows
itself to be cloned. We can call the Object.clone() method on only on
objects whose classes implement the Cloneable interface. If we attempt
to invoke the clone() method on an object of a class that does not
implement the Cloneable interface, we get a CloneNotSupportedException.
Also note that the clone()
method is a protected method, so you will most likely need to create a
public method on your bean class named clone() to mimic the
functionality.
We are going to demonstrate both
the above methods using a simple Employee class. This class will
contain an instance of another javabean 'Address'. We shall see in the
below example, how we can obtain a deep copy of our beans.
The following code demonstrates the usage of Object.clone()
Address.java
class Address {
private int houseNo;
public int getHouseNo() {
return houseNo;
}
public void setHouseNo(int houseNo) {
this.houseNo = houseNo;
}
@Override
public String toString() {
return "houseNo : " + houseNo;
}
}
Employee.java
class Employee implements Cloneable {
private String name = null;
private Address address=null;
@Override
public String toString() {
return "name " + this.getName()+ " address : "+ address;
}
public Employee clone() {
Employee emp = null;
try {
emp = (Employee) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e);
}
return emp;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
Note that in the above classes, the
Employee class and the Address class is declared with a visibility of
default. We did not have to make them public, although we could have.
Also note the way the clone()
method has been written in the Employee class. I explicitly declared it
as as a public method and called the superclass implementation of the
clone method. Then, I downcasted it to am Employee object before
returning.
Lets see the code in action.
public static void main(String[] args) {
Employee emp1 = new Employee();
Address add1= new Address();
add1.setHouseNo(100);
emp1.setName("ryan");
emp1.setAddress(add1);
Employee emp2 = emp1.clone();
emp2.setName("ryan2");
print("emp1 : " + emp1);
print("emp2 : " + emp2);
print("emp1==emp2 "+(emp1==emp2));
}
If you execute the following code, you will get the below output
emp1 : name ryan address : houseNo : 100
emp2 : name ryan2 address : houseNo : 100
emp1==emp2 false
You can replace the print statement with your logger statement to run the code.
Note
that the == operator indicates that both the objects are created
independently on the heap. Moreover, the fields of the Address bean have
also been copied.
One crucial thing to be noted
here is that you only needed to implement the Cloneable interface in the
Employee class. The Address class does not need to implement Cloneable,
although there wont be any serious repercussions if you do so!
Now lets see the second method
Using the BeanUtils.cloneBean() class
This method makes use of the BeanUtils
class provided by the apache foundation. In order to use this class,
you need to have at least the following jar files in your classpath
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-logging-1.0.4.jar
The version numbers may differ, but you can get the details from the
here if the dependencies change.
The BeanUtils class provides us a
method called cloneBean that clones all the accessible properties of
your beans. Here is the code in Action.
Employee2.classpublic class Employee2{
private String name = null;
private Address address=null;
@Override
public String toString() {
return "name " + this.getName()+ " address : "+ address;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Note the declaration of the Employee2
class. We did not implement the Cloneable interface. Moreover, we made
the class "public". Making the class public is required for the
BeanUtils class to extract the data from the Bean. Also note that we did
not have to write a clone() function in this class.
We can reuse the existing Address class from the previous example, as no changes need to be done to it.
You need to import the following line in your main class
import org.apache.commons.beanutils.BeanUtils;
Now lets take a look at the main function.
public static void main(String[] args) throws Exception{
BeanUtils bu = new BeanUtils();
Employee2 emp1 = new Employee2();
Address add1= new Address();
add1.setHouseNo(100);
emp1.setName("ryan");
emp1.setAddress(add1);
Employee2 emp2 = (Employee2)bu.cloneBean(emp1);
emp2.setName("??");
print(emp1);
print(emp2);
print("emp1==emp2 : "+(emp1==emp2));
}
As you see above, we did not have to
do much but simply call the cloneBean method on the BeanUtils object and
downcast it to our Employee2 bean. As was expected, a deep copy of the
object was created.
If you run the code, you get the following output
name ryan address : houseNo : 100
name ?? address : houseNo : 100
emp1==emp2 : false
As expected, both the objects are
considered to be different objects on the heap. They just have the same
values for their properties.
In the methods discussed above,
you can see that the BeanUtils method can be used in a much wider scope
because most of your JavaBeans will be public, but you may not always
have access to the code of your JavaBeans to write a clone method.
Thats all for now folks!
Happy Programming :)
From http://mycodefixes.blogspot.com/2011/02/creating-deep-copy-of-javabeans.html
Comments