java.io
Interface Serializable

All Known Subinterfaces:
AdapterActivator sample code for org.omg.PortableServer.AdapterActivator definition code for org.omg.PortableServer.AdapterActivator , Attribute sample code for javax.print.attribute.Attribute definition code for javax.print.attribute.Attribute , Attribute sample code for javax.naming.directory.Attribute definition code for javax.naming.directory.Attribute , Attributes sample code for javax.naming.directory.Attributes definition code for javax.naming.directory.Attributes , BindingIterator sample code for org.omg.CosNaming.BindingIterator definition code for org.omg.CosNaming.BindingIterator , ClientRequestInfo sample code for org.omg.PortableInterceptor.ClientRequestInfo definition code for org.omg.PortableInterceptor.ClientRequestInfo , ClientRequestInterceptor sample code for org.omg.PortableInterceptor.ClientRequestInterceptor definition code for org.omg.PortableInterceptor.ClientRequestInterceptor , Codec sample code for org.omg.IOP.Codec definition code for org.omg.IOP.Codec , CodecFactory sample code for org.omg.IOP.CodecFactory definition code for org.omg.IOP.CodecFactory , Control sample code for javax.naming.ldap.Control definition code for javax.naming.ldap.Control , Current sample code for org.omg.CORBA.Current definition code for org.omg.CORBA.Current , Current sample code for org.omg.PortableInterceptor.Current definition code for org.omg.PortableInterceptor.Current , Current sample code for org.omg.PortableServer.Current definition code for org.omg.PortableServer.Current , CustomValue sample code for org.omg.CORBA.portable.CustomValue definition code for org.omg.CORBA.portable.CustomValue , DataInputStream sample code for org.omg.CORBA.DataInputStream definition code for org.omg.CORBA.DataInputStream , DataOutputStream sample code for org.omg.CORBA.DataOutputStream definition code for org.omg.CORBA.DataOutputStream , Descriptor sample code for javax.management.Descriptor definition code for javax.management.Descriptor , DocAttribute sample code for javax.print.attribute.DocAttribute definition code for javax.print.attribute.DocAttribute , DomainManager sample code for org.omg.CORBA.DomainManager definition code for org.omg.CORBA.DomainManager , DSAPrivateKey sample code for java.security.interfaces.DSAPrivateKey definition code for java.security.interfaces.DSAPrivateKey , DSAPublicKey sample code for java.security.interfaces.DSAPublicKey definition code for java.security.interfaces.DSAPublicKey , DynAny sample code for org.omg.DynamicAny.DynAny definition code for org.omg.DynamicAny.DynAny , DynAnyFactory sample code for org.omg.DynamicAny.DynAnyFactory definition code for org.omg.DynamicAny.DynAnyFactory , DynArray sample code for org.omg.DynamicAny.DynArray definition code for org.omg.DynamicAny.DynArray , DynEnum sample code for org.omg.DynamicAny.DynEnum definition code for org.omg.DynamicAny.DynEnum , DynFixed sample code for org.omg.DynamicAny.DynFixed definition code for org.omg.DynamicAny.DynFixed , DynSequence sample code for org.omg.DynamicAny.DynSequence definition code for org.omg.DynamicAny.DynSequence , DynStruct sample code for org.omg.DynamicAny.DynStruct definition code for org.omg.DynamicAny.DynStruct , DynUnion sample code for org.omg.DynamicAny.DynUnion definition code for org.omg.DynamicAny.DynUnion , DynValue sample code for org.omg.DynamicAny.DynValue definition code for org.omg.DynamicAny.DynValue , DynValueBox sample code for org.omg.DynamicAny.DynValueBox definition code for org.omg.DynamicAny.DynValueBox , DynValueCommon sample code for org.omg.DynamicAny.DynValueCommon definition code for org.omg.DynamicAny.DynValueCommon , ECPrivateKey sample code for java.security.interfaces.ECPrivateKey definition code for java.security.interfaces.ECPrivateKey , ECPublicKey sample code for java.security.interfaces.ECPublicKey definition code for java.security.interfaces.ECPublicKey , ExtendedRequest sample code for javax.naming.ldap.ExtendedRequest definition code for javax.naming.ldap.ExtendedRequest , ExtendedResponse sample code for javax.naming.ldap.ExtendedResponse definition code for javax.naming.ldap.ExtendedResponse , Externalizable sample code for java.io.Externalizable definition code for java.io.Externalizable , IdAssignmentPolicy sample code for org.omg.PortableServer.IdAssignmentPolicy definition code for org.omg.PortableServer.IdAssignmentPolicy , IDLEntity sample code for org.omg.CORBA.portable.IDLEntity definition code for org.omg.CORBA.portable.IDLEntity , IDLType sample code for org.omg.CORBA.IDLType definition code for org.omg.CORBA.IDLType , IdUniquenessPolicy sample code for org.omg.PortableServer.IdUniquenessPolicy definition code for org.omg.PortableServer.IdUniquenessPolicy , ImplicitActivationPolicy sample code for org.omg.PortableServer.ImplicitActivationPolicy definition code for org.omg.PortableServer.ImplicitActivationPolicy , Interceptor sample code for org.omg.PortableInterceptor.Interceptor definition code for org.omg.PortableInterceptor.Interceptor , IORInfo sample code for org.omg.PortableInterceptor.IORInfo definition code for org.omg.PortableInterceptor.IORInfo , IORInterceptor sample code for org.omg.PortableInterceptor.IORInterceptor definition code for org.omg.PortableInterceptor.IORInterceptor , IORInterceptor_3_0 sample code for org.omg.PortableInterceptor.IORInterceptor_3_0 definition code for org.omg.PortableInterceptor.IORInterceptor_3_0 , IRObject sample code for org.omg.CORBA.IRObject definition code for org.omg.CORBA.IRObject , Key sample code for java.security.Key definition code for java.security.Key , LifespanPolicy sample code for org.omg.PortableServer.LifespanPolicy definition code for org.omg.PortableServer.LifespanPolicy , Name sample code for javax.naming.Name definition code for javax.naming.Name , NamingContext sample code for org.omg.CosNaming.NamingContext definition code for org.omg.CosNaming.NamingContext , NamingContextExt sample code for org.omg.CosNaming.NamingContextExt definition code for org.omg.CosNaming.NamingContextExt , NotificationFilter sample code for javax.management.NotificationFilter definition code for javax.management.NotificationFilter , ObjectReferenceFactory sample code for org.omg.PortableInterceptor.ObjectReferenceFactory definition code for org.omg.PortableInterceptor.ObjectReferenceFactory , ObjectReferenceTemplate sample code for org.omg.PortableInterceptor.ObjectReferenceTemplate definition code for org.omg.PortableInterceptor.ObjectReferenceTemplate , ORBInitializer sample code for org.omg.PortableInterceptor.ORBInitializer definition code for org.omg.PortableInterceptor.ORBInitializer , ORBInitInfo sample code for org.omg.PortableInterceptor.ORBInitInfo definition code for org.omg.PortableInterceptor.ORBInitInfo , POA sample code for org.omg.PortableServer.POA definition code for org.omg.PortableServer.POA , POAManager sample code for org.omg.PortableServer.POAManager definition code for org.omg.PortableServer.POAManager , Policy sample code for org.omg.CORBA.Policy definition code for org.omg.CORBA.Policy , PolicyFactory sample code for org.omg.PortableInterceptor.PolicyFactory definition code for org.omg.PortableInterceptor.PolicyFactory , PrintJobAttribute sample code for javax.print.attribute.PrintJobAttribute definition code for javax.print.attribute.PrintJobAttribute , PrintRequestAttribute sample code for javax.print.attribute.PrintRequestAttribute definition code for javax.print.attribute.PrintRequestAttribute , PrintServiceAttribute sample code for javax.print.attribute.PrintServiceAttribute definition code for javax.print.attribute.PrintServiceAttribute , PrivateKey sample code for java.security.PrivateKey definition code for java.security.PrivateKey , PublicKey sample code for java.security.PublicKey definition code for java.security.PublicKey , QueryExp sample code for javax.management.QueryExp definition code for javax.management.QueryExp , RelationType sample code for javax.management.relation.RelationType definition code for javax.management.relation.RelationType , RemoteRef sample code for java.rmi.server.RemoteRef definition code for java.rmi.server.RemoteRef , RequestInfo sample code for org.omg.PortableInterceptor.RequestInfo definition code for org.omg.PortableInterceptor.RequestInfo , RequestProcessingPolicy sample code for org.omg.PortableServer.RequestProcessingPolicy definition code for org.omg.PortableServer.RequestProcessingPolicy , RSAMultiPrimePrivateCrtKey sample code for java.security.interfaces.RSAMultiPrimePrivateCrtKey definition code for java.security.interfaces.RSAMultiPrimePrivateCrtKey , RSAPrivateCrtKey sample code for java.security.interfaces.RSAPrivateCrtKey definition code for java.security.interfaces.RSAPrivateCrtKey , RSAPrivateKey sample code for java.security.interfaces.RSAPrivateKey definition code for java.security.interfaces.RSAPrivateKey , RSAPublicKey sample code for java.security.interfaces.RSAPublicKey definition code for java.security.interfaces.RSAPublicKey , RunTime sample code for org.omg.SendingContext.RunTime definition code for org.omg.SendingContext.RunTime , ServantActivator sample code for org.omg.PortableServer.ServantActivator definition code for org.omg.PortableServer.ServantActivator , ServantLocator sample code for org.omg.PortableServer.ServantLocator definition code for org.omg.PortableServer.ServantLocator , ServantManager sample code for org.omg.PortableServer.ServantManager definition code for org.omg.PortableServer.ServantManager , ServantRetentionPolicy sample code for org.omg.PortableServer.ServantRetentionPolicy definition code for org.omg.PortableServer.ServantRetentionPolicy , ServerRef sample code for java.rmi.server.ServerRef definition code for java.rmi.server.ServerRef , ServerRequestInfo sample code for org.omg.PortableInterceptor.ServerRequestInfo definition code for org.omg.PortableInterceptor.ServerRequestInfo , ServerRequestInterceptor sample code for org.omg.PortableInterceptor.ServerRequestInterceptor definition code for org.omg.PortableInterceptor.ServerRequestInterceptor , StreamableValue sample code for org.omg.CORBA.portable.StreamableValue definition code for org.omg.CORBA.portable.StreamableValue , SupportedValuesAttribute sample code for javax.print.attribute.SupportedValuesAttribute definition code for javax.print.attribute.SupportedValuesAttribute , ThreadPolicy sample code for org.omg.PortableServer.ThreadPolicy definition code for org.omg.PortableServer.ThreadPolicy , UnsolicitedNotification sample code for javax.naming.ldap.UnsolicitedNotification definition code for javax.naming.ldap.UnsolicitedNotification , ValueBase sample code for org.omg.CORBA.portable.ValueBase definition code for org.omg.CORBA.portable.ValueBase , ValueExp sample code for javax.management.ValueExp definition code for javax.management.ValueExp

public interface Serializable

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

 private void writeObject(java.io.ObjectOutputStream out)
     throws IOException
 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException;
 

The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The default mechanism for saving the Object's fields can be invoked by calling out.defaultWriteObject. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

The readObject method is responsible for reading from the stream and restoring the classes fields. It may call in.defaultReadObject to invoke the default mechanism for restoring the object's non-static and non-transient fields. The defaultReadObject method uses information in the stream to assign the fields of the object saved in the stream with the correspondingly named fields in the current object. This handles the case when the class has evolved to add new fields. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

Serializable classes that need to designate an alternative object to be used when writing an object to the stream should implement this special method with the exact signature:

 ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
 

This writeReplace method is invoked by serialization if the method exists and it would be accessible from a method defined within the class of the object being serialized. Thus, the method can have private, protected and package-private access. Subclass access to this method follows java accessibility rules.

Classes that need to designate a replacement when an instance of it is read from the stream should implement this special method with the exact signature.

 ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
 

This readResolve method follows the same invocation rules and accessibility rules as writeReplace.

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException sample code for java.io.InvalidClassException definition code for java.io.InvalidClassException . A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

 ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
 
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members.

Since:
JDK1.1
See Also:
ObjectOutputStream sample code for java.io.ObjectOutputStream definition code for java.io.ObjectOutputStream , ObjectInputStream sample code for java.io.ObjectInputStream definition code for java.io.ObjectInputStream , ObjectOutput sample code for java.io.ObjectOutput definition code for java.io.ObjectOutput , ObjectInput sample code for java.io.ObjectInput definition code for java.io.ObjectInput , Externalizable sample code for java.io.Externalizable definition code for java.io.Externalizable