DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Creating a Copy of JavaBeans

Creating a Copy of JavaBeans

Ryan Sukale user avatar by
Ryan Sukale
·
Feb. 28, 11 · Interview
Like (0)
Save
Tweet
Share
23.95K Views

Join the DZone community and get the full member experience.

Join For Free

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.

  1. Using Object.clone()
  2. 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.class
public 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

 

Object (computer science) Bean (software) Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 10 Best Practices for Web Application Testing
  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective
  • A First Look at Neon
  • Old School or Still Cool? Top Reasons To Choose ETL Over ELT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: