如果一个实现了 Serializable 的类没有 serialVersionUID 属性,IDE(比如Eclipse)通常会报这样一个 warning:

    1. The serializable class Foo does not declare a static final
    2. serialVersionUID field of type long

    那这个 serialVersionUID 是做什么用的呢?可以看看JDK中 Serializable 接口的注释:

    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 {@link 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:
    

    serialVersionUID 表示可序列化类的版本, 在反序列化对象时, 用来确认序列化与反序列化该对象所使用的类的版本是否兼容. 如果类的版本不一致, 那么反序列化将不能正常进行, 抛出InvalidClassException.

    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. Array classes cannot declare an explicit serialVersionUID,
    so they always have the default computed value, but the requirement
    for matching serialVersionUID values is waived for array classes.
    

    如果一个可序列化的类没有包含 serialVersionUID, 运行时会根据这个类的特征自动计算出一个serialVersionUID.
    那么, 为什么不能用默认的这个实现呢, 似乎更省事? 因为不同的编译器实现会导致同一个类的源代码文件, 被计算出不同的 serialVersionUID.