Search This Blog

Wednesday, 29 January 2014

clone method in java

/* program to illustrate clone() method */


package clonemethod;
class Student implements Cloneable
{
    int roll;
    String name;
    Student(int roll,String name)
    {
        this.roll=roll;
        this.name=name;
        System.out.println("details are : "+roll+" "+name);
    }
public Object clone()throws CloneNotSupportedException{  
return super.clone();  
}  

public static void main(String args[]){  
try{  
Student s1=new Student(21,"sanjay");  
  
Student s2=(Student)s1.clone();  
  
System.out.println(s1.roll+" "+s1.name);  
System.out.println(s2.roll+" "+s2.name);  
  
}catch(CloneNotSupportedException c){}  
  
}  
}  

output

details are : 21 sanjay
21 sanjay
21 sanjay

No comments:

Popular Posts