Wednesday 25 November 2015

Object Cloning in java example?

package com.javaproficiency;

public class Emp implements Cloneable {
//Object Cloning in java example?

int id;
String name;

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

public Emp clone() throws CloneNotSupportedException{

return (Emp ) super.clone();

}

public static void main(String[] args) {

Emp  emp = new Emp(10, "java");
Emp emp1 = null;
try {
emp1 = emp.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(" clone object is ");
System.out.println("id ="+ emp1.id+ " name ="+ emp1.name);

}

}


Output:

 clone object is
 id =10 name =java




3 comments: