Java Reference
In-Depth Information
When the serialized hash map is deserialized, the two analogous entries
in the new copy of the hash map will have references to a single copy of
[2]
The first
key
field is the word "rose" in Tibetan.
Sometimes, however, sharing objects in this way is not what is desired.
In that case you can use
ObjectOutputStream
's
writeUnshared
method to
write the object as a new distinct object, rather than using a reference
to an existing serialization of that object. Any object written into the
graph by
writeUnshared
will only ever have one reference to it in the seri-
alized data. The
readUnshared
method of
ObjectInputStream
reads an ob-
ject that is expected to be unique. If the object is actually a reference to
an existing deserialized object then an
ObjectStreamException
is thrown;
similarly, if the deserialization process later tries to create a second ref-
erence to an object returned by
readUnshared
, an
ObjectStreamException
is
thrown. These uniqueness checks only apply to the actual object passed
to
writeUnshared
or read by
readUnshared
, not to any objects they refer to.
20.8.2. Making Your Classes
Serializable
When an
ObjectOutputStream
writes a serialized object, the object must
implement the
Serializable
marker interface. This marker interface de-
clares that the class is designed to have its objects serialized.