How to Copy Object Properties in Java using Common BeanUtils

Copy Object Properties in Java

In this tutorial, I am attaching Simple java example program about, How to Copy Object Properties in Java by using Apache Common BeanUtils API. The example program were tested with environment and output has been shared in the same post.

In this example, I am using few apache libraries such as Common BeanUtils and Common Logging. You can refer the project structure screen shot, Which is attached below.

Project Structure

copy object properties in java

Note: Both Common BeanUtils and Common Logging libraries should be available in your project, In order to perform Copy Object Properties in Java. These libraries can be downloaded from Apache Official Site. The appropriate link has been shared below.

1. Apache Common BeanUtils API
2. Apache Common Logging API

Customer.java

Customer.java, This is the Simple Java Bean class. Which has Properties as well as Getters and Setters accordingly.

package com.dineshkrish;

public class Customer {

	private int customerId;
	private String customerName;
	private int customerAge;

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public int getCustomerAge() {
		return customerAge;
	}

	public void setCustomerAge(int customerAge) {
		this.customerAge = customerAge;
	}

}

Application.java

package com.dineshkrish;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class Application {

	public static void main(String[] args) {

		// Defining Customer Object (ie: c2) with setting property
		Customer c1 = new Customer();

		c1.setCustomerId(1001);
		c1.setCustomerName("Dinesh Krishnan");
		c1.setCustomerAge(29);

		// Defining Customer Object (ie: c2) with setting property
		Customer c2 = new Customer();

		try {

			// Coping Properties
			BeanUtils.copyProperties(c2, c1);

			System.out.println("All Properties has been copied from c1 object to c2 Object");

		} catch (IllegalAccessException e) {

			System.out.println(e.getMessage());
			e.printStackTrace();
		} catch (InvocationTargetException e) {

			System.out.println(e.getMessage());
			e.printStackTrace();
		}

		System.out.println("\n-------------------------------");

		System.out.println("Accessing c2 Object Properties\n");
		
		System.out.println("Customer ID : " + c2.getCustomerId());
		System.out.println("Customer Name : " + c2.getCustomerName());
		System.out.println("Customer Age : " + c2.getCustomerAge());

	}
	
}

Output

—————
All Properties has been copied from c1 object to c2 Object

——————————-
Accessing c2 Object Properties

Customer ID : 1001
Customer Name : Dinesh Krishnan
Customer Age : 29

Reference

1. Apache Common BeanUtils API
2. Apache Common BeanUtils API Documentation