I can't seem to find a good explaination of the answer to the following question: If two objects with non-"transient" references to each other are serialized using ObjectOutputStream, will this create an infinite loop? Explain why or why not. I hope that someone can throw some light on this. Regards Nigel [ March 16, 2004: Message edited by: Nigel Browne ]
The short answer is "no". I'm not sure of the serialisation algorithm details, but my understanding is that as objects are serialised, they are assigned a value (a 'serial number'), which is used by other serialised objects to reference them. Thus a circular reference in serial format is much the same as it is in the equivalent objects: there is a circular reference, but unless you try to navigate it (naively) there is no problem. I realise this isn't the most enlightening post; perhaps one of the more experienced members can add detail. The code below demonstrates this. I'd hoped to post the serialised object code as well, but it's pretty meaningless without both a hex viewer and a deep understanding of the serial format.
Joined: May 15, 2001
Thanks for your reply. I had come to the same answer, but am still confused to how the locking mechanism works. All that is mentioned in the API is
Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.
I find this sentance a little hard to understand. Regards Nigel
Hi If you have a JDK installation and look at the file - ObjectInputStream.java and go to the readObject Method - with a little bit of more searching and going through the file you will be able to understand that there is a call in this method - called - handles.markDependency(outerHandle, passHandle); which marks the dependency and keeps it in a list and saves it so that the parent class is not written again. Sumit