Let’s say I have objects of type A,B and C. I have 3 Maps which contain all instances of A,B and C respectively. Internally, both A and B have Maps of C. I want to be able to store and restore the state of the application at any time.
So, until today I had always serialized pyramid-like applications, where I would call serialize on the top Object, and the call would propagate to everything else. How do I deal with this situation? If I call serialize on the A map and then on the B map, aren’t C instances going to be saved twice? Even if they do, will the deserialization leave the application state as it was just overwriting C instances when I deserialize the B map after deserializing the A map?
Thanks in advance.
The Java serialization mechanism knows about multiple references to the same object and won’t duplicate them. The object will be stored once, and all internal references will be kept.
After deserialization, your objects will be in the same state: only one instance and multiple references to that object.