Google News
logo
JSP - Interview Questions
Write an example of the clone method.
Syntax :
class FullName implements Cloneable{ 
     String Firstname;
     String Lastname; 
     FullName(String Firstname,String Lastname){ 
          this.Firstname=Firstname; 
          this.Lastname=Lastname; 
     } 
    public Object clone()throws CloneNotSupportedException
{ 
          return super.clone(); 
     } 
     public static void main(String args[])
{ 
          Try
{ 
               FullName s1=new FullName("Free Time","Learning"); 
               FullName s2=(FullName)s1.clone(); 
          System.out.println(s1.Firstname+" "+s1.Lastname); 
          System.out.println(s2.Firstname+" "+s2.Lastname); 
          }
          catch(CloneNotSupportedException c)
{
            } 
     } 
}
The above code will print the string “Free Time Learning” twice.
Advertisement