Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Util Clone method example in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Clone method example in Java

Advertisement
Given example of java clone() method illustrates, how to use clone() method.

Clone method example in Java

     

Clone method example in Java programming language

Given example of java clone() method illustrates, how to use clone() method. The Cloneable interface defines a method called Clone(), which can be used to clone an object in your java program.

If you are trying to use the clone method in a class where Cloneable interface is not implemented, it throws CloneNotSupportedException. Clone() method is used to create and return copy of the object.

Clone method example code

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(20081231);
  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 of Clone method example:

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]

Download code example discussed in this section.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 12, 2009

Related Tutorials

Discuss: Clone method example in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Mohit
September 1, 2011
Errors

mohit-sharmas-macbook-pro:java programs mohitsharma$ javac CloneTest.java CloneTest.java:8: inconvertible types found : Object required: Employee Employee emp1 = (Employee)emp.clone(); ^ CloneTest.java:24: incompatible types found : Employee required: Object return cloned; ^ 2 errors
MANOJ KUMAR KANOJIA
December 4, 2012
core java

very simple example
zhang
April 24, 2013
about clone method

clone method is not defined in Cloneable interface! it's in Object.
DMCA.com