Copy any Object to an Object in Android

 If you don't want to use any copy contructor and manually write element by element assignment, you can use following snippet to copy an object.
  1. public static Object copy(Object orig) {  
  2.     Object obj = null;  
  3.     try {  
  4.         // Write the object out to a byte array  
  5.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  6.         ObjectOutputStream out = new ObjectOutputStream(bos);  
  7.         out.writeObject(orig);  
  8.         out.flush();  
  9.         out.close();  
  10.   
  11.         // Make an input stream from the byte array and read  
  12.         // a copy of the object back in.  
  13.         ObjectInputStream in = new ObjectInputStream(  
  14.                 new ByteArrayInputStream(bos.toByteArray()));  
  15.         obj = in.readObject();  
  16.     } catch (IOException e) {  
  17.         e.printStackTrace();  
  18.     } catch (ClassNotFoundException cnfe) {  
  19.         cnfe.printStackTrace();  
  20.     }  
  21.     return obj;  

How to use this function??
  1. ClassEntity copyEntity = (ClassEntity) copy(classEntityObj);