Cloning an Object in Java

Object cloning is the process of creating  exact copy of an object,clone() method of Object class is can be used to clone an object.

The Class whose object is to be cloned must implement java.lang.Cloneable interface.

class Student implements Cloneable{
int studentId;
String studentName;

Student(int id,String name){

public Object clone()throws CloneNotSupportedException{
return super.clone();

public static void main(String args[]){
Student s=new Student(555,"vijay");

Student s2=(Student)s.clone();

System.out.println(s.studentId+" "+s.studentName);
System.out.println(s2.studentId+" "+s2.studentName);

}catch(CloneNotSupportedException c){


This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s