Java Reference
In-Depth Information
Normally, an object is serialized as itself on the output stream, and a
copy of the same type is reconstituted during deserialization. You will
find a few classes for which this is not correct. For example, if you have
a class that has objects that are supposed to be unique in each virtual
machine for each unique value (so that == will return TRue if and only if
equals also would return true ), you would need to resolve an object be-
ing deserialized into an equivalent one in the local virtual machine. You
can control these by providing writeReplace and readResolve methods of
the following forms and at an appropriate access level:
<access> Object writeReplace() throws ObjectStreamException
Returns an object that will replace the current object during
serialization. Any object may be returned including the cur-
rent one.
<access> Object readResolve() tHRows ObjectStreamException
Returns an object that will replace the current object during
deserialization. Any object may be returned including the cur-
rent one.
In our example, readResolve would check to find the local object that
was equivalent to the one just deserializedif it exists it will be returned,
otherwise we can register the current object (for use by readResolve in
the future) and return this . These methods can be of any accessibility;
they will be used if they are accessible to the object type being serial-
ized. For example, if a class has a private readResolve method, it only
affects deserialization of objects that are exactly of its type. A package-
accessible readResolve affects only subclasses within the same package,
while public and protected readResolve methods affect objects of all sub-
classes.
 
Search WWH ::




Custom Search