---
title: "What is object cloning in java?"  
description: "What is object cloning in java?"  
author: "Samuel Fernandes"  
published: 2015-03-30  
updated: 2020-09-16  
canonical: https://www.mindstick.com/interview/2374/what-is-object-cloning-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# What is object cloning in java?

The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object.

The **java.lang.Cloneable** interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.

**Why clone method? :** The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning.

**The clone() method** is defined in the Object class. Syntax of the clone() method is as follows:

protected Object clone() throws CloneNotSupportedException

## Example:

```
class Student18 implements Cloneable{  int rollno;  String name;    Student18(int rollno,String name){  this.rollno=rollno;  this.name=name;  }    public Object clone()throws CloneNotSupportedException{  return super.clone();  }    public static void main(String args[]){  try{  Student18 s1=new Student18(101,"amit");    Student18 s2=(Student18)s1.clone();    System.out.println(s1.rollno+" "+s1.name);  System.out.println(s2.rollno+" "+s2.name);    }catch(CloneNotSupportedException c){}    }  }  
```

## Answers

### Answer by Anonymous User

The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object.

The **java.lang.Cloneable** interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.

**Why clone method? :** The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning.

**The clone() method** is defined in the Object class. Syntax of the clone() method is as follows:

protected Object clone() throws CloneNotSupportedException

## Example:

```
class Student18 implements Cloneable{  int rollno;  String name;    Student18(int rollno,String name){  this.rollno=rollno;  this.name=name;  }    public Object clone()throws CloneNotSupportedException{  return super.clone();  }    public static void main(String args[]){  try{  Student18 s1=new Student18(101,"amit");    Student18 s2=(Student18)s1.clone();    System.out.println(s1.rollno+" "+s1.name);  System.out.println(s2.rollno+" "+s2.name);    }catch(CloneNotSupportedException c){}    }  }  
```


---

Original Source: https://www.mindstick.com/interview/2374/what-is-object-cloning-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
