Java Reference
In-Depth Information
many development systems, it is the output of a command called
seri-
alver
. Other systems have different ways to provide you with this value,
which is the serial version
UID
of the class before the first incompatible
modification. (Nothing prevents you from using any number as this
UID
if you stamp it from the start, but it is usually a really bad idea. Your
numbers will not be as carefully calculated to avoid conflict with other
classes as the secure hash is.)
Now when the
ObjectInputStream
finds your class and compares the
UID
with that of the older version in the file, the
UID
s will be the same even
though the implementation has changed. If you invoke
defaultReadOb-
ject
, only those fields that were present in the original version will be
set. Other fields will be left in their default state. If
writeObject
in the
earlier version of the class wrote values on the field without using
de-
faultWriteObject
, you must read those values. If you try to read more
values than were written, you will get an
EOFException
, which can inform
you that you are deserializing an older form that wrote less informa-
tion. If possible, you should design classes with a class version number
instead of relying on an exception to signal the version of the original
data.
When an object is written to an
ObjectOutputStream
, the
Class
object for
that object is also written. Because
Class
objects are specific to each vir-
tual machine, serializing the actual
Class
object would not be helpful. So
Class
objects on a stream are replaced by
ObjectStreamClass
objects that
contain the information necessary to find an equivalent class when the
object is deserialized. This information includes the class's full name and
its serial version
UID
. Unless you create one, you will never directly see
an
ObjectStreamClass
object.
As a class evolves it is possible that a new superclass is introduced for
that class. If an older serialized form of the class is deserialized it will
not contain any serialized data for that superclass. Rather than making
this an error, the system will set all fields declared by the superclass to
their default initialized values. To override this default behavior, the new
superclass (which must implement
Serializable
, of course) can declare
the following method: