Paid for writing

In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks

Tuesday, January 27, 2009

Object Cloning in Java

// CloneEg.java

public class CloneEg
{
public static void main(String args[])
{
Employee e1=new Employee("Id 1","Name 1",42000);
Employee e2= (Employee) e1.clone();

System.out.println("Id : " +e2.id);
System.out.println("Name : " +e2.name);
System.out.println("Salary : "+e2.salary);
}
}

class Employee
{
String id, name;
double salary;

public Employee(String id, String name, double salary)
{
this.id=id;
this.name=name;
this.salary=salary;
}


public Object clone()
{
Employee cloneEmp;

cloneEmp=new Employee(this.id, this.name, this.salary);

return cloneEmp;
}
}

/*
Output ---

Id : Id 1
Name : Name 1
Salary : 42000.0

*/

0 comments: