/**
* Serializability of a classis enabled bytheclass 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. <p>
*
* To allow subtypes of non-serializable classes to be serialized, the
* subtype may assume responsibility for saving and restoring the
* state ofthe supertype's public, protected, and (if accessible)
* package fields. The subtype may assume this responsibility only if
* theclassit extends has an accessible no-arg constructor to
* initialize theclass's state. It is an errorto declare a class
* Serializable if this isnotthe case. The error will be detected at
* runtime. <p>
*
* During deserialization, the fields of non-serializable classes will
* be initialized using the public or protected no-arg constructor of
* theclass. A no-arg constructor must be accessible tothe subclass
* thatis serializable. The fields of serializable subclasses will
* be restored fromthe stream. <p>
*
* When traversing a graph, an object may be encountered thatdoesnot
* support the Serializable interface. In this case the
* NotSerializableException will be thrown and will identify theclass
* ofthe non-serializable object. <p>
*
* Classes that require special handling during the serialization and
* deserialization process must implement special methods with these exact
* signatures:
*
* <PRE>
* private void writeObject(java.io.ObjectOutputStream out)
* throws IOException
* private void readObject(java.io.ObjectInputStream in)
* throws IOException, ClassNotFoundException;
* private void readObjectNoData()
* throws ObjectStreamException;
* </PRE>
*
* <p>The writeObject method is responsible for writing the state ofthe
* object forits particular class so thatthe corresponding
* readObject method can restore it. The default mechanism for saving
* the Object's fields can be invoked by calling
* out.defaultWriteObject. The method doesnot need to concern
* itself withthe state belonging toits superclasses or subclasses.
* State is saved by writing the individual fields tothe
* ObjectOutputStream using the writeObject method orby using the
* methods for primitive data types supported by DataOutput.
*
* <p>The readObject method is responsible for reading fromthe 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 ofthe object saved inthe stream withthe
* correspondingly named fields inthe current object. This handles the case
* when theclass has evolved to add new fields. The method doesnot need to
* concern itself withthe state belonging toits superclasses or subclasses.
* State is saved by writing the individual fields tothe
* ObjectOutputStream using the writeObject method orby using the
* methods for primitive data types supported by DataOutput.
*
* <p>The readObjectNoData method is responsible for initializing the state of
* the object forits particular classinthe event thatthe serialization
* stream doesnotlistthegivenclassas a superclass ofthe object being
* deserialized. This may occur in cases wherethe receiving party uses a
* different versionofthe deserialized instance's class than the sending
* party, andthe receiver's version extends classes that are not extended by
* the sender's version. This may also occur ifthe serialization stream has
* been tampered; hence, readObjectNoData is useful for initializing
* deserialized objects properly despite a "hostile"or incomplete source
* stream.
*
* <p>Serializable classes that need to designate an alternative object to be
* used when writing an object tothe stream should implement this
* special method withthe exact signature:
*
* <PRE>
* ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException;
* </PRE><p>
*
* This writeReplace method is invoked by serialization ifthe method
* exists andit would be accessible from a method defined within the
* classofthe object being serialized. Thus, the method can have private,
* protected and package-private access. Subclass access to this method
* follows java accessibility rules. <p>
*
* Classes that need to designate a replacement when an instance ofit
* isreadfromthe stream should implement this special method withthe
* exact signature.
*
* <PRE>
* ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException;
* </PRE><p>
*
* This readResolve method follows the same invocation rules and
* accessibility rules as writeReplace.<p>
*
* The serialization runtime associates with each serializable class a version
* number, called a serialVersionUID, which is used during deserialization to
* verify thatthe sender and receiver of a serialized object have loaded
* classes forthat object that are compatible with respect to serialization.
* If the receiver has loaded a classforthe object that has a different
* serialVersionUID than thatofthe corresponding sender's class, then
* deserialization will resultin an {@link InvalidClassException}. A
* serializable class can declare its own serialVersionUID explicitly by
* declaring a field named <code>"serialVersionUID"</code> that must be static,
* final, andof type <code>long</code>:
*
* <PRE>
* ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
* </PRE>
*
* If a serializable classdoesnot explicitly declare a serialVersionUID, then
* the serialization runtime will calculate a default serialVersionUID value
* forthatclass based on various aspects oftheclass, as described inthe
* Java(TM) Object Serialization Specification. However, itis <em>strongly
* recommended</em> that all serializable classes explicitly declare
* serialVersionUID values, sincethe default serialVersionUID computation is
* highly sensitive toclass details that may vary depending on compiler
* implementations, and can thus resultin unexpected
* <code>InvalidClassException</code>s 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 <code>private</code> modifier where
* possible, since such declarations apply only tothe immediately declaring
* class--serialVersionUID fields are not useful as inherited members. Array
* classes cannot declare an explicit serialVersionUID, so they always have
* the default computed value, butthe requirement for matching
* serialVersionUID values is waived for array classes.
*
* @author unascribed
* @see java.io.ObjectOutputStream
* @see java.io.ObjectInputStream
* @see java.io.ObjectOutput
* @see java.io.ObjectInput
* @see java.io.Externalizable
* @since JDK1.1
*/
public interface Serializable {
}
Serializablejava version “1.8.0_131”一、总结Serializable 接口是一个标记接口,接口中无方法定义作用:一个 Class 实现了 Seriablizable 接口,表明该类声明的对象可以转为一个字节序列,保存对象的状态保存对象的状态的作用:可以快速恢复原来对象的状态(对象的状态指的是某一时刻的对象的属性赋值,如 Person p