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 java.io.*;import java.util.*;class Employee implements Cloneable{    private int no;    public Employee(int no)    {	this.no=no;    }     public Object clone()    {        try        {            return super.clone();        }        catch(Exception e)        {            return null;        }    }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: