clone ?

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

thanks
ShoGunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.