3 Replies Latest reply on Dec 15, 2009 7:18 AM by 843798

    Replace object's content with another object's content


      In my current problem, I have 2 objects whose type are known at runtime.

      Object a;
      Object b;

      What I want to do is to replace the content of Object a with the content of Object b but I cannot do a = b; because there are other part of program that is holding a reference to Object a and it will require a lot of modification to update the reference. I looked into 2 possible solutions for this method.

      1. I use reflection to iterate through Object a' fields and set each of them individually. However, Java access control is preventing me from setting the value of non-public fields.

      2. Serialization: I read a couple of article but it only allow me to create a "new" instance of the same object not replacing the current content of an existing one.

      Do you have any suggestion?