The Java object clone() method creates exactly the same copy of the object. It implements the cloneable class of Java to create the exact copy of the input provided. This method provides an easy method of copying the objects. The objects can be copied without writing codes repeatedly.
This write-up will demonstrate in detail the usage of the clone() method of Java.
How to Use/Utilize the Java Object clone() Method?
The clone() method of Java replicates the objects and gives the exact copy including all the parameters. There are certain methods for the implementation of Java clone() object like the Shallow copy which is the default cloning method in Java that makes the copies of primitive data types excluding object references whereas in deep copy both the data types and object references are copied.
The implementation of two methods has been discussed in this article.
Method 1:Implementation of clone() Method Using Shallow Copy
This method is by default a method of clone() object that copies all the objects.
// Built-in class cloneable of Java
class Main implements Cloneable{
// strings and integers are declared
String employeename;
int employeeid;
String employeepost;
String employeeadd;
public static void main(String[] args) {
//Declare values respectively in the Main class
Main object1 = new Main();
object1.employeename = "Mr Adam";
object1.employeeid = 3489;
System.out.println(object1.employeename);
System.out.println(object1.employeeid);
// Declare values respectively in the Main Class
Main object3= new Main();
// Declare input for the objects crteated
object3.employeeadd="Block D street 29,circular road";
object3.employeepost="Banker";
// Print the output
System.out.println(object3.employeeadd);
System.out.println(object3.employeepost);
// try catch block for exception handling
try {
// Clone method on object 2 along with the outputs
Main object2 = (Main)object1.clone();
System.out.println(object2.employeename);
System.out.println(object2.employeeid);
// Clone method on object 4 along with the respective outputs
Main object4 = (Main)object3.clone();
System.out.println(object4.employeeadd);
System.out.println(object4.employeepost);
}
catch (Exception o) {
System.out.println(o);
}
}
}
In the above code:
- A built-in class of Java that is cloneable has been called.
- The objects in the form of strings and integers have been declared.
- Using the clone() method in the try block the objects declared will be cloned exactly like the input provided.
- The catch block has been used as an exception.
Output

In the above output, the same objects have been printed since the clone() method makes the copy of the original one.
Method 2: Implementation of clone() Method Using Deep Copy
The deep copy is used in such a manner that a new array is created and all the values present in that array are copied.
class Post implements Cloneable{
int employeeid;
String employeepost;
//Declaring respective integers and strings
public Post(int employeeid,String employeepost) {
this.employeeid = employeeid;
this.employeepost = employeepost;
}//Exception for the clone() method.
protected Object clone() throws CloneNotSupportedException {//return the clone() value.
return super.clone();
}
}
//Cloneable class
class newEmployee implements Cloneable {
int empid;
String emppost;
public newEmployee(int empid, String emppost) {
this.empid = empid;
this.emppost = emppost;
}
protected Object clone() throws CloneNotSupportedException {//The clone() method to create a copy.
newEmployee emp = (newEmployee) super.clone();
return emp;
}
}
//Creating Deep copy class
class DeepCopyInJava {
public static void main(String[] args) {
newEmployee empl1 = new newEmployee(111, "Operation Manager");
newEmployee empl2 = null;
try {
// Assigning clone of empl1 to empl2
empl2 = (newEmployee) empl1.clone();
} catch (CloneNotSupportedException f) {
f.printStackTrace();
}
// Printing the results
System.out.println(empl1.emppost);
empl2.emppost = "Manager";
System.out.println(empl1.emppost);
}
}
In the above code:
- A cloneable class of name Post has been created.
- Both integer and string values have been passed for the cloning.
- Another cloneable class of name newEmployee has been created that also contains an integer and string value.
- The respective integer and string values for the first and second classes have been declared in the DeepcopyJava class.
- In the try block, the clone for the values has been created.
- The exception handling has been declared in the catch block.
- The values however have been changed to Manager.
- Since the value has been cloned, therefore, the output will appear as the cloned value.
Output
In the following output despite changing the value of the object that is post in empl2 it has not affected the results.

The implementation of the clone() method in Java comes to an end.
Conclusion
The clone() method in Java is responsible for creating exactly the same copy of the object that has been provided as the input. In this article, we have effectively demonstrated the way to use the clone() method using shallow copy and deep copy in Java.