Javatpoint Logo
Javatpoint Logo

Cloneable in Java

Cloneable is an interface that is used to create the exact copy of an object. It exists in java.lang package. A class must implement the Cloneable interface if we want to create the clone of the class object.

The clone() method of the Object class is used to create the clone of the object. However, if the class doesn't support the cloneable interface, then the clone() method generates the CloneNotSupportedException.

The syntax of the clone() method is given below.

We can also create a copy of an object by using the new keyword, but it will take a lot of processing time. Therefore, using the clone() method is efficient for this purpose. Consider the following example to create a copy of an object using the clone() method.

Example

Output:

101 John
101 John

In the above example, the reference variables s and s1 contains the same values, and the object s is copied to another object by the clone() method.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA