Thursday, March 12, 2009

When a Serializable object is written with writeObject, then modified and

Question :When a Serializable object is written with writeObject, then modified and
written a second time, why is the modification missing when the stream is
deserialized? (CoreJava)

Answer :The ObjectOutputStream class keeps track of each object it serializes and
sends only the handle if the object is written into the stream a subsequent
time. This is the way it deals with graphs of objects. The corresponding
ObjectInputStream keeps track of all of the objects it has created and their
handles so when the handle is seen again it can return the same object.
Both output and input streams keep this state until they are freed.
Alternatively, the ObjectOutputStream class implements a reset method
that discards the memory of having sent an objecct, so sending an object
again will make a copy.

No comments: