您的位置:首页 > 产品设计 > UI/UE

private static final long serialVersionUID = ???

2013-01-01 14:53 387 查看
private static final long serialVersionUID = ???

此变量有什么用途?

一般在什么情况下加入这样的变量?它的值有什么要求?

你可以这样理解:

serialVersionUID 用来表明类的不同版本间的兼容性。如果你修改了此类, 要修改此值。否则以前用老版本的类序列化的类恢复时会出错。为了在反序列化时,确保类版本的兼容性,最好在每个要序列化的类中加入private static final long serialVersionUID这个属性,具体数值自己定义。

在警告上点左键就可以自动添加。当然也可以去掉警告。设置:Window-->Preferences-->Java,将serializable class without serialVersionUID的设置由warning改为Ignore。然后Eclipse会重新编译程序,那些警告信息也就消失了。

google。得到答案如下:

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.

以上是Serializable在javadoc中的描述。

也既是说:

serialVersionUID用来作为Java对象序列化中的版本标示之用;

如果一个序列化类没有声明这样一个static final的产量,JVM会根据各种参数为这个类计算一个;

对于同样一个类,不同版本的JDK可能会得出不同的serivalVersionUID;

The serializable class XXX does not declare a static final serialVersionUID field of type long

serialVersionUID作用:

序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。

你可以随便写一个,在Eclipse中它替你生成一个,有两种生成方式:

一个是默认的1L,比如:private static final long serialVersionUID = 1L;

一个是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如:private static final long serialVersionUID = -8940196742313994740L;之类的。

当你一个类实现了Serializable接口,如果没有定义serialVersionUID,Eclipse会提供这个提示功能告诉你去定义之。

在Eclipse中点击类中warning的图标一下,Eclipse就会自动给定两种生成的方式,如上面所述。如果不想定义它,在Eclipse的设置中也可以把它关掉的,设置如下:

Window ==> Preferences ==> Java ==> Compiler ==> Error/Warnings ==> Potential programming problems

将Serializable class without serialVersionUID的warning改成ignore即可。

如果你没有考虑到兼容性问题时,就把它关掉,不过有这个功能是好的,只要任何类别实现了Serializable这个接口的话,如果没有加入 serialVersionUID,Eclipse都会给你warning提示,这个serialVersionUID为了让该类别 Serializable向后兼容。

如果你的类Serialized存到硬盘上面后,可是后来你却更改了类别的field(增加或减少或改名),当你Deserialize时,就会出现Exception的,这样就会造成不兼容性的问题。

但当serialVersionUID相同时,它就会将不一样的field以type的预设值Deserialize,这个可以避开不兼容性的问题。

最后,不考虑兼容性的问题,我把这个warning改成ignore了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: