3 Replies Latest reply: Dec 14, 2009 9:21 PM by jtahlborn RSS

    Replace object's content with another object's content

    843798
      Hi,

      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?