cloneable in Java

By: Saravanan Emailed: 1649 times Printed: 2122 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)
How to use ArrayList in Java
FileReader and FileWriter example program in Java
How to use Iterator in Java
The Basic Structure of a Simple Java program
Hashtable example in Java
Using One-Dimensional Arrays in Java
Read from a COM port using Java program
append() in Java
Integer: byte, short, int, and long data types in Java
Method Overloading (function overloading) in Java
compareTo( ) in Java
Recursion in java
wait(), notify() and notifyAll() in Java - A tutorial
Vector example in Java
Most Emailed Articles (in Java)
How to use regionMatches( ) in Java
Java Transaction API 1.0
emulator 0 terminated while waiting for it to register!
Distributed Multi-tiered Applications
Creating Users and Passwords with Tomcat using tomcat-users.xml
Java String
this keyword sample in Java
instanceof operator example in Java
Java File
Setting Up SSL on Tomcat
TCP Server and TCP Client in Java
Read from a COM port using Java program
HTTPS Java - URL Connection program. Using HttpsURLConnection
What is Multithreaded programming?
Calculate average sale of the week in Java