CONTENTS | PREV | NEXT | Java Object Serialization Specification |
For serializable objects, thereadObject
method allows a class to control the deserialization of its own fields. Here is its signature:private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException;Each subclass of a serializable object may define its ownreadObject
method. If a class does not implement the method, the default serialization provided bydefaultReadObject
will be used. When implemented, the class is only responsible for restoring its own fields, not those of its supertypes or subtypes.The
readObject
method of the class, if implemented, is responsible for restoring the state of the class. The values of every field of the object whether transient or not, static or not are set to the default value for the fields type. EitherObjectInputStream
'sdefaultReadObject
orreadFields
method must be called once (and only once) before reading any optional data written by the correspondingwriteObject
method; even if no optional data is read,defaultReadObject
orreadFields
must still be invoked once. If thereadObject
method of the class attempts to read more data than is present in the optional part of the stream for this class, the stream will return-1
for bytewise reads, throw an EOFException for primitive data reads (e.g.,readInt
,readFloat
), or throw anOptionalDataException
with theeof
field set totrue
for object reads.The responsibility for the format, structure, and versioning of the optional data lies completely with the class. The
@serialData
javadoc tag within the javadoc comment for thereadObject
method should be used to document the format and structure of the optional data.If the class being restored is not present in the stream being read, then its
readObjectNoData
method, if defined, is invoked (instead ofreadObject
); otherwise, its fields are initialized to the appropriate default values. For further detail, see section 3.5.Reading an object from the
ObjectInputStream
is analogous to creating a new object. Just as a new object's constructors are invoked in the order from the superclass to the subclass, an object being read from a stream is deserialized from superclass to subclass. ThereadObject
orreadObjectNoData
method is called instead of the constructor for eachSerializable
subclass during deserialization.One last similarity between a constructor and a
readObject
method is that both provide the opportunity to invoke a method on an object that is not fully constructed. Any overridable (neither private, static nor final) method called while an object is being constructed can potentially be overridden by a subclass. Methods called during the construction phase of an object are resolved by the actual type of the object, not the type currently being initialized by either its constructor orreadObject
/readObjectNoData
method. Therefore, calling an overridable method from within areadObject
orreadObjectNoData
method may result in the unintentional invocation of a subclass method before the superclass has been fully initialized.