• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

clone ?

can anybody give me code on how to do a clone of the object in Java?

thanks
0
ShoGun
Asked:
ShoGun
  • 2
1 Solution
 
rainmalCommented:
what you want is called a deep copy in java,

from this page
http://java.sun.com/people/linden/faq_d.html

16.(Sect. 18) How can I clone using serialization?

      Look at the code below, submitted by expert programmer John Dumas. It uses
    serialization to write an object into a byte array, and reads it back to reconstitute a fresh
    copy. This is a clever hack!


    import java.io.ByteArrayOutputStream;
    import java.io.ByteArrayInputStream;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
       
    public class Cloner {
       private Cloner() {}
       
       public static Object cloneObject(Object o) throws Exception {
          ByteArrayOutputStream bOut = new ByteArrayOutputStream();
          ObjectOutputStream out     = new ObjectOutputStream(bOut);
       
          out.writeObject(o);
       
          ByteArrayInputStream bIn =
                   new ByteArrayInputStream(bOut.toByteArray());
          ObjectInputStream in     = new ObjectInputStream(bIn);
       
          return(in.readObject());
       }
       
       public static void main(String args[]) throws Exception {
          java.util.Vector v = new java.util.Vector();
          v.addElement(new StringBuffer("Hello"));
       
          java.util.Vector vClone =
                     (java.util.Vector)Cloner.cloneObject(v);
       
          // Changing the StringBuffer int the cloned vector has no
          // effect on the original StringBuffer object --
          // demonstrating that we have indeed done a deep copy
       
          ((StringBuffer)vClone.elementAt(0)).append(" world");
       
          StringBuffer sb = (StringBuffer)v.elementAt(0);
          System.out.println(sb.toString());
       
          sb = (StringBuffer)vClone.elementAt(0);
          System.out.println(sb.toString());
       
          int array[] = { 1, 2, 3, 4, 5 };
       
          int arrayClone[] = (int [])Cloner.cloneObject(array);
       
          // Again, changes to an element in the cloned array do not
          // have any effect on the original
       
          arrayClone[0]++;
       
          System.out.println(array[0]);
          System.out.println(arrayClone[0]);
       }
    }

    The main() routine is just a driver. All the cleverness is in the very brief cloneObject(). It
    does a "deep" clone, which is what you usually want (though Java gives you a "shallow"
    clone by default).


and also look at this page
http://www.javaworld.com/javaworld/javatips/jw-javatip76.html
0
 
ShoGunAuthor Commented:
hey i was waiting for soemtime and how come the comment time is before my post time???
0
 
ShoGunAuthor Commented:
thansk I tried out and worked but can tell me why clone() is unused for cloning here?
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now