Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

clone method in Java

clone() method in Java is used to create and return copy of the object. Clone() method is used in class where Cloneable interface is implemented but throws a CloneNotSupportedException where a Cloneable interface is not implemented.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

clone() method in Java is used to create and return copy of the object. Clone() method is used in class where Cloneable interface is implemented but throws a CloneNotSupportedException where a Cloneable interface is not implemented.

public clone() method is not specified in many interfaces and abstract classes. Also it's use in against abstraction.

If you want to retrieve an object of clone() method, you must cast it in the same class in the appropriate type.

One can use copy constructor instead of clone() method.

In the following example we will learn how clone() method works and is used.

import java.util.*;

public class CloneTest{
  public static void main(String[] args){

  Employee emp = new Employee("Amardeep", 50000);
  emp.setHireDay(2005,0,0);
  Employee emp1 = (Employee)emp.clone();
  emp1.raiseSalary(20);
  emp1.setHireDay(2008, 12, 31);
  System.out.println("Employee=" + emp);
  System.out.println("copy=" + emp1);
  }
}
class Employee implements Cloneable{
  public Employee(String str, double dou){
  name = str;
  salary = dou;
  }
  public Object clone(){
  try{
  Employee cloned = (Employee)super.clone();
  cloned.hireDay = (Date)hireDay.clone();
  return cloned;
  }
  catch(CloneNotSupportedException e){
  System.out.println(e);
  return null;
  }
  }
  public void setHireDay(int year, int month, int day){
  hireDay = new GregorianCalendar(year,month - 1, day).getTime();
  }
  public void raiseSalary(double byPercent){
  double raise = salary * byPercent/100;
  salary += raise;
  }
  public String toString(){
  return "[name=" + name+ ",salary=" + salary+ ",hireDay=" + hireDay+ "]";
  }
  private String name;
  private double salary;
  private Date hireDay;
}

Output:

C:\help>javac CloneTest.java

C:\help>java CloneTest
Employee=[name=Amardeep, salary=50000.0, hireDay=Tue Nov 30 00:00:00 GMT+05:30 2004]
copy=[name=Amardeep, salary=60000.0, hireDay=Wed Dec 31 00:00:00 GMT+05:30 2008]

Advertisements

Ads
Share on Google+Share on Google+

clone method in Java

Posted on: May 15, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: clone method in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads