clone ?

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

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.

what you want is called a deep copy in java,

from this page

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!

    public class Cloner {
       private Cloner() {}
       public static Object cloneObject(Object o) throws Exception {
          ByteArrayOutputStream bOut = new ByteArrayOutputStream();
          ObjectOutputStream out     = new ObjectOutputStream(bOut);
          ByteArrayInputStream bIn =
                   new ByteArrayInputStream(bOut.toByteArray());
          ObjectInputStream in     = new ObjectInputStream(bIn);
       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 =
          // 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);
          sb = (StringBuffer)vClone.elementAt(0);
          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

    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

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???
ShoGunAuthor Commented:
thansk I tried out and worked but can tell me why clone() is unused for cloning here?
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

From novice to tech pro — start learning today.