devxlogo

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 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;        }    }}

size=3>

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist