Brody
Brody

Reputation: 1

When serializaing a collection of objects that each contains a reference to another object in the collection, what will it do?

So, all of the objects are being serialized, but each one contains a reference to another object(of the same type) in the collection. I think I should name it transient, store a string that could be used to get the object again upon deserialization, then restore it at that point. In that case though, would I do that in the overwritten readObject method? If that explanation makes sense, can anyone help? Thanks.

Upvotes: 0

Views: 86

Answers (1)

Michael Krussel
Michael Krussel

Reputation: 2656

There's no need to do this. Java's serialization can handle circular references. Both ObjectInputStream and ObjectObjectStream keeps track of objects that they have already processed to prevent infinite serialization loops. This can lead to a memory leak if you hold on to the streams for a long time.

Upvotes: 1

Related Questions