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 definition code for , Attributes sample code for definition code for , 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 definition code for , 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 definition code for , DSAPublicKey sample code for definition code for , 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 definition code for , ECPublicKey sample code for definition code for , 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 definition code for , 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 definition code for , 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 definition code for , 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 definition code for , PublicKey sample code for definition code for , QueryExp sample code for definition code for , RelationType sample code for definition code for , 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 definition code for , RSAPrivateCrtKey sample code for definition code for , RSAPrivateKey sample code for definition code for , RSAPublicKey sample code for definition code for , 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 definition code for

public interface Serializable

Serializability of a class is enabled by the class implementing the 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( out)
     throws IOException
 private void readObject( 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 definition code for . 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.

See Also:
ObjectOutputStream sample code for definition code for , ObjectInputStream sample code for definition code for , ObjectOutput sample code for definition code for , ObjectInput sample code for definition code for , Externalizable sample code for definition code for