站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

Java Object Serialization Specification: 1 - System Architecture - JDK 5 Documentation v1.2.2, Java 2 SDK 英文文档

CONTENTS | PREV | NEXT Java Object Serialization Specification


1.10 The Serializable Interface

Object Serialization produces a stream with information about the JavaTM classes for the objects which are being saved. For serializable objects, sufficient information is kept to restore those objects even if a different (but compatible) version of the implementation of the class is present. The Serializable interface is defined to identify classes which implement the serializable protocol:

package java.io;

public interface Serializable {};
A Serializable class must do the following:

  • Implement the java.io.Serializable interface
  • Identify the fields that should be serializable
    (Use the serialPersistentFields member to explicitly declare them serializable or use the transient keyword to denote nonserializable fields.)
  • Have access to the no-arg constructor of its first nonserializable superclass
The class can optionally define the following methods:

  • A writeObject method to control what information is saved or to append additional information to the stream
  • A readObject method either to read the information written by the corresponding writeObject method or to update the state of the object after it has been restored
  • A writeReplace method to allow a class to nominate a replacement object to be written to the stream
    (See Section 2.5, "The writeReplace Method" for additional information.)
  • A readResolve method to allow a class to designate a replacement object for the object just read from the stream
    (See Section 3.6, "The readResolve Method" for additional information.)
ObjectOutputStream and ObjectInputStream allow the serializable classes on which they operate to evolve (allow changes to the classes that are compatible with the earlier versions of the classes). See Section 5.5, "Compatible JavaTM Type Evolution" for information about the mechanism which is used to allow compatible changes.



CONTENTS | PREV | NEXT
Copyright © 1997-1998 Sun Microsystems, Inc. All Rights Reserved.