什么是 serialVersionUID,我为什么要使用它? [英] What is a serialVersionUID and why should I use it?

查看:47
本文介绍了什么是 serialVersionUID,我为什么要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eclipse 在 serialVersionUID 缺失时发出警告.

<块引用>

可序列化类 Foo 未声明静态 final类型为 long 的 serialVersionUID 字段

什么是serialVersionUID?它为什么重要?请举例说明缺少 serialVersionUID 会导致问题.

解决方案

java.io.Serializable 可能和你得到的解释一样好:

<块引用>

序列化运行时为每个可序列化类关联一个版本号,称为 serialVersionUID,在反序列化期间使用它来验证序列化对象的发送者和接收者是否已为该对象加载了类与序列化兼容.如果接收者为对象加载了一个类,该对象的 serialVersionUID 与相应的发送者的类不同,那么反序列化将导致InvalidClassException.一个可序列化的类可以通过声明一个名为 serialVersionUID 的字段来显式声明它自己的 serialVersionUID,该字段必须是静态的、最终的,并且类型为 long:>

<块引用>

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

<块引用>

如果可序列化类没有显式声明 serialVersionUID,那么序列化运行时将根据类的各个方面计算该类的默认 serialVersionUID 值,如在 Java(TM) 对象序列化规范中进行了描述.但是,强烈建议所有可序列化的类都明确声明 serialVersionUID 值,因为默认的 serialVersionUID 计算对可能变化的类细节高度敏感取决于编译器的实现,因此可能会在反序列化期间导致意外的 InvalidClassExceptions.因此,为了保证在不同的 Java 编译器实现中具有一致的 serialVersionUID 值,可序列化的类必须声明一个显式的 serialVersionUID 值.还强烈建议显式 serialVersionUID 声明尽可能使用 private 修饰符,因为此类声明仅适用于立即声明的类 — serialVersionUID 字段作为继承成员没有用.

Eclipse issues warnings when a serialVersionUID is missing.

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

What is serialVersionUID and why is it important? Please show an example where missing serialVersionUID will cause a problem.

解决方案

The docs for java.io.Serializable are probably about as good an explanation as you'll get:

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. 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.

这篇关于什么是 serialVersionUID,我为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆