First create your pojo class that implement Colonable Interface like below code
package com.myapp.concepts;
public class Employee implements Cloneable {
private String name;
private String designation;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Employee() {
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Address {
private String city;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
private String state;
public Address() {
}
}
Now test your Object before cloning and after cloning. There is magic behind the scene check out: For theory go to javapapers.com for practical come to my blog
package com.myapp.concepts;
public class ObjectCloneTest2 {
public static void main(String s[]) {
Employee emp = new Employee();
emp.setName("James");
emp.setDesignation("Programmer");
Address address = new Address();
address.setCity("Nehru Place");
address.setState("Delhi state");
emp.setAddress(address);
System.out.println("Before cloning of Employee");
System.out.println(emp.getName());
System.out.println(emp.getDesignation());
System.out.println(emp.getAddress().getCity());
System.out.println(emp.getAddress().getState());
System.out
.println("After cloning of Employee i m changing the address with cloned object");
try {
Employee clonedEmp = (Employee) emp.clone();
// changing the refered object of address with cloned object
clonedEmp.setName("Siddharth");
clonedEmp.getAddress().setCity("Noida");
clonedEmp.getAddress().setState("UP");
System.out.println("Cloned object details are:");
System.out.println(clonedEmp.getName());
System.out.println(clonedEmp.getDesignation());
System.out.println(clonedEmp.getAddress().getCity());
System.out.println(clonedEmp.getAddress().getState());
System.out
.println("After changing with cloned object now address of Original object is:");
System.out.println(emp.getName());
System.out.println(emp.getDesignation());
System.out.println(emp.getAddress().getCity());
System.out.println(emp.getAddress().getState());
} catch (CloneNotSupportedException cnse) {
System.out.println("Cloneable should be implemented. " + cnse);
}
}
}
package com.myapp.concepts;
public class Employee implements Cloneable {
private String name;
private String designation;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Employee() {
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Address {
private String city;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
private String state;
public Address() {
}
}
Now test your Object before cloning and after cloning. There is magic behind the scene check out: For theory go to javapapers.com for practical come to my blog
package com.myapp.concepts;
public class ObjectCloneTest2 {
public static void main(String s[]) {
Employee emp = new Employee();
emp.setName("James");
emp.setDesignation("Programmer");
Address address = new Address();
address.setCity("Nehru Place");
address.setState("Delhi state");
emp.setAddress(address);
System.out.println("Before cloning of Employee");
System.out.println(emp.getName());
System.out.println(emp.getDesignation());
System.out.println(emp.getAddress().getCity());
System.out.println(emp.getAddress().getState());
System.out
.println("After cloning of Employee i m changing the address with cloned object");
try {
Employee clonedEmp = (Employee) emp.clone();
// changing the refered object of address with cloned object
clonedEmp.setName("Siddharth");
clonedEmp.getAddress().setCity("Noida");
clonedEmp.getAddress().setState("UP");
System.out.println("Cloned object details are:");
System.out.println(clonedEmp.getName());
System.out.println(clonedEmp.getDesignation());
System.out.println(clonedEmp.getAddress().getCity());
System.out.println(clonedEmp.getAddress().getState());
System.out
.println("After changing with cloned object now address of Original object is:");
System.out.println(emp.getName());
System.out.println(emp.getDesignation());
System.out.println(emp.getAddress().getCity());
System.out.println(emp.getAddress().getState());
} catch (CloneNotSupportedException cnse) {
System.out.println("Cloneable should be implemented. " + cnse);
}
}
}