CONTENTS | PREV | NEXT | Java Object Serialization Specification |
For serializable objects, thewriteObject
method allows a class to control the serialization of its own fields. Here is its signature:private void writeObject(ObjectOutputStream stream) throws IOException;Each subclass of a serializable object may define its ownwriteObject
method. If a class does not implement the method, the default serialization provided bydefaultWriteObject
will be used. When implemented, the class is only responsible for writing its own fields, not those of its supertypes or subtypes.The class's
writeObject
method, if implemented, is responsible for saving the state of the class. EitherObjectOutputStream
'sdefaultWriteObject
orwriteFields
method must be called once (and only once) before writing any optional data that will be needed by the correspondingreadObject
method to restore the state of the object; even if no optional data is written,defaultWriteObject
orwriteFields
must still be invoked once. IfdefaultWriteObject
orwriteFields
is not invoked once prior to the writing of optional data (if any), then the behavior of instance deserialization is undefined in cases where theObjectInputStream
cannot resolve the class which defined thewriteObject
method in question.The responsibility for the format, structure, and versioning of the optional data lies completely with the class.