» » Object cloning in java example

  1. package com.instanceofjava;
  2.  
  3. public class Employee implements Cloneable {
  4.  
  5.         int a=0; 
  6.         String name="";
  7.         Employee (int a,String name){ 
  8.         this.a=a;
  9.         this.name=name;
  10. }
  11.  
  12. public Employee clone() throws CloneNotSupportedException{
  13.  
  14. return (Employee ) super.clone();
  15.  
  16. }
  17.  
  18. public static void main(String[] args) {
  19.  
  20.           Employee e=new Employee (2,"Indhu");
  21.             System.out.println(e.name);
  22.  
  23. try {
  24.  
  25.  Employee b=e.clone();
  26.  System.out.println(b.name);
  27.  
  28. }
  29.  catch (CloneNotSupportedException e1) {

  30. e1.printStackTrace();
  31. }
  32. }
  33.  
  34. }




Output:
  1. Indhu
  2. Indhu

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu