1

I am very new and learning java,I want to perform deep cloning without serialization ,I read some articles from internet and still in doubt about deep cloning without serialization.So i want to know is there any other rules that I have to follow to do deep cloning, below is my program

Department.java

package com.deepclone;

public class Department {
    private int id;
    private String name;


    public Department(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



}

Employee.java

package com.deepclone;

public class Employee implements Cloneable {
    private String employeeId;
    private String empName;
    private Department department;

    public Employee(String employeeId, String empName, Department department) {
        this.employeeId = employeeId;
        this.empName = empName;
        this.department = department;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Employee employee = new Employee(employeeId, empName, new Department(
                department.getId(), department.getName()));
        return employee;
    }

    public String getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

}

TestCloning.java

    package com.deepclone;

public class TestClonning1 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Department hrDepartment = new Department(10, "HR");
        Employee employee = new Employee("1", "rajeev", hrDepartment);
        System.out.println(employee.getDepartment().getName());
        Employee cloneEmployee = (Employee) employee.clone();
        System.out.println(cloneEmployee.getDepartment().getName());
        cloneEmployee.getDepartment().setName("it");
        System.out.println(employee.getDepartment().getName());
        System.out.println(cloneEmployee.getDepartment().getName());
    }
}

output

HR
HR
HR
it

is there any other alternative to achive deep cloning without serialization...if yes then give link.

rajeev pani..
  • 5,387
  • 5
  • 27
  • 35
  • 1
    I don't see you doing any serialization?! – Smutje Jul 21 '14 at 11:07
  • without serialization any other way.......easy way – rajeev pani.. Jul 21 '14 at 11:09
  • Seems like more of a philosophical question without practical usage, as deep cloning always presents disadvantages and pitfalls. – Smutje Jul 21 '14 at 11:11
  • 1
    I highly recommend checking out - http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java - First. As stated above cloning presents ALOT of issues and if you're not experienced you'll end up plucking your own hair out – Juxhin Jul 21 '14 at 11:13

1 Answers1

0

Try this Java Deep-Cloning Library

Cloner cloner = new Cloner();
MyClass other = ...;
MyClass clone = cloner.deepClone(other);
alex
  • 8,904
  • 6
  • 49
  • 75