cloneable in Java

By: Saravanan Emailed: 1697 times Printed: 2196 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 )
InetAddress Example program in Java
XML and Java - Parsing XML using Java Tutorial
How to Send SMS using Java Program (full code sample included)
Inheritance Example in Java
Type Casting in Java
Simple Port Scanner application using Java
right shift operator, >>, in Java
Garbage collection and Finalize() method
Read from a COM port using Java program
Abstract classes in Java
Integer: byte, short, int, and long data types in Java
Student marks calculation program in Java
Calculate gross salary in Java
Stack example in Java - push(), pop(), empty(), search()
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Most Emailed Articles (in Java)
Write to a COM port using Java program
How to use ArrayList in Java
Java program for changeable wrapper class
History of Java
Data Types in Java
Sample program to demonstrate the use of ActionListener
Introducing JavaScript.
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Student marks calculation program in Java
Vector in Java - Sample Program
Save/Write/Read image file from/to a database using Java program
How to access instance from an inner class and accessing outer class variables in java
Method Override sample in Java
Using jar utility in Java - A jar tutorial