Making a Clone of a Java Object

Making a Clone of a Java Object

You cannot make a separate location to assign one object to another. By implementing a Cloneable interface and calling the clone() method, you can make a separate location for the same object. Implement your class with Cloneable and when you want to copy just call the clone() method.

 import*;import java.util.*;class Employee implements Cloneable{    private int no;    public Employee(int no)    {;    }     public Object clone()    {        try        {            return super.clone();        }        catch(Exception e)        {            return null;        }    }}



Share the Post: