cloneable in Java

By: Saravanan Emailed: 1743 times Printed: 2432 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

In this tutorial we are going to see use of cloneable interface and how to use it. The cloneable interface defines no members. It is used to indicate that a class allows a bitwise copy of an object to be made. If you try to call clone() on a class that does not implement Cloneable, a CloneNotSupportedException is thrown. When a clone is made the constructor for the object being cloned is not called. A clone is a copy of the original.

class Customer implements Cloneable 
{
  String name;
  int income;
  public Customer(String name, int income) 
  {
    this.name = name;
    this.income = income;
  }

  public Customer() 
  {
  }

  public String getName() 
  {
    return name;
  }

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

  public void setIncome(int income) 
  {
    this.income = income;
  }

  public int getIncome() 
  {
    return this.income;
  }

  public Object clone() throws CloneNotSupportedException 
  {
    try 
    {
      return super.clone();
    } 
    catch (CloneNotSupportedException cnse) 
    {
      System.out.println("CloneNotSupportedException thrown " + cnse);
      throw new CloneNotSupportedException();
    }
  }
}

public class MainClass 
 {
  public static void main(String[] args) 
  {
    try 
   {
      Customer c= new Customer("Angel", 9000);
      System.out.println(c);
      System.out.println("The Customer name is " + c.getName());
      System.out.println("The Customer pay is " + c.getIncome());

      Customer cClone = (Customer) c.clone();
      System.out.println(cClone);
      System.out.println("The clone's name is " + cClone.getName());
      System.out.println("The clones's pay is " + cClone.getIncome());

    } 
   catch (CloneNotSupportedException cnse) 
   {
      System.out.println("Clone not supported");
    }
  }
}

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Saravanan
Converting properties using PropertyEditors and Other Spring features worth mentioning
How to create an array and method in JSP
inheritance in Java
Use List, Set, SortedSet, LinkedHashSet, Map, LinkedHashMap in java
cloneable in Java
Java program for Associate keys with values
Java code for Enumeration and using java.lang.reflect.Array
Insert an element in Array, Search and Sort Array by using java program
Type Casting in Java
How to initialize an Array and how to copy the array
Simple java program to add an element to specified index of Java ArrayList
How to use set, get basic and nested properties for Spring framework
How to access instance from an inner class and accessing outer class variables in java
How to use and access the inner class in java
Java program for Map

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
XML and Java - Parsing XML using Java Tutorial
What is Java?
The Basic Structure of a Simple Java program
Stack example in Java - push(), pop(), empty(), search()
Count number of vowels, consonants and digits in a String in Java
Using StringTokenizer in Java
Student marks calculation program in Java
wait(), notify() and notifyAll() in Java - A tutorial
FileReader and FileWriter example program in Java
InetAddress Example program in Java
append() in Java
Integer: byte, short, int, and long data types in Java
Method Overriding in Java
Read from a COM port using Java program
Most Emailed Articles (in Java)
Update contents of a file within a jar file
Java Chat Client Sample Applet
Execute system commands in a Java Program
Get user input in Java
Reading from a file and writing to a file using Java program
ThreadGroup Sample in Java
String Concatenation using Java
J2EE Services
Insert, Delete, Search, Print an int Array in Java
Text to Speech conversion program in Java
Major components of FxObjects in JavaFX
How to use and access the inner class in java
History of Java
Importing a Certificate for the CA example using keytool in Java
Using continue Statement - example in Java