Cloning in java


Click here to download eclipse supported ZIP file


Description

The java.lang.Object.clone() creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

 x.clone() != x

will be true, and that the expression:

x.clone().getClass() == x.getClass()

will be true, but these are not absolute requirements. While it is typically the case that:

 x.clone().equals(x)

will be true, this is not an absolute requirement.

Declaration

Following is the declaration for java.lang.Object.clone() method

protected Object clone()

Parameters

  • NA

Return Value

This method returns a clone of this instance.

Exception

  • CloneNotSupportedException -- if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.

Example

The following example shows the usage of lang.Object.clone() method.


Let us compile and run the above program, this will produce the following result:

Mon Sep 17 04:51:41 EEST 2012
Mon Sep 17 04:51:41 EEST 2012

 

    

package com.cv.java.clone;

/**
 @author Chandra Vardhan
 *
 */
public class CloneNotSupport {

  /**
   @param args
   */
  public static void main(String[] args) {

    Student2 student1 = new Student2();
    student1.setName("kodam");
    student1.setId("205");
    System.out.println(student1);

    try {
      Student2 student2 = (Student2student1.clone();
      System.out.println(student2);

    catch (CloneNotSupportedException e) {
     System.err.println("Cloing not supported here...");
    }

  }

}


 

    
/**
 
 */
package com.cv.java.clone;

/**
 @author Chandra Vardhan
 *
 */
public class CloneTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Student student1 = new Student();
    student1.setName("kodam");
    student1.setId("205");
    System.out.println(student1);

    try {
      Student student2 = (Studentstudent1.clone();
      System.out.println(student2);

      student2.setName("netha");
      student2.setId("206");

      System.out.println(student1);
      System.out.println(student2);

      student1.setName("nara");
      student1.setId("207");

      System.out.println(student1);
      System.out.println(student2);

    catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }

  }

}


 

    

package com.cv.java.clone;

/**
 @author Chandra Vardhan
 *
 */
import java.util.GregorianCalendar;

public class CloneTest2 {

  public static void main(String[] args) {

    // create a gregorian calendar, which is an object
    GregorianCalendar cal = new GregorianCalendar();

    // clone object cal into object y
    GregorianCalendar y = (GregorianCalendarcal.clone();

    // print both cal and y
    System.out.println("" + cal.getTime());
    System.out.println("" + y.getTime());
  }
}


 

    
package com.cv.java.clone;

import java.io.Serializable;

/**
 @author Chandra Vardhan
 
 */
public class Student implements Cloneable, Serializable {

  private String name;
  private String id;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "Student [name=" + name + ", id=" + id + "]";
  }

  @Override
  protected Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
}


 

    
package com.cv.java.clone;

/**
 @author Chandra Vardhan
 
 */
public class Student2 {

  private String name;
  private String id;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }


  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }


  @Override
  public String toString() {
    return "Student [name=" + name + ", id=" + id + "]";
  }
  
  @Override
  protected Object clone() throws CloneNotSupportedException {
    return super.clone();
  }
  
}

No comments:

Post a Comment