Java中的序列化版本uid [英] Serialization version uid in Java

查看:118
本文介绍了Java中的序列化版本uid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

序列化ID如何存储在对象的实例中?

How is Serialization id stored in the instance of the object ?

我们在Java中声明的序列化ID是静态字段;静态字段不是序列化的。

The Serialization id we declare in Java is static field;and static fields are not serialized.

然后应该有一些方法来存储静态最终字段。 java是如何做到的?

There should be some way to store the static final field then. How does java do it ?

推荐答案

serialVersionUID 未存储在序列化的实例中object,因为它是一个静态字段(它是类的一部分,不是对象的一部分)。

The serialVersionUID is not stored in the instance of a "serialized" object, as it is an static field (it is part of the class, not part of the object).

因此,它是存储的在编译的字节码中,如果它实际定义,否则计算。在 java规范中:

Therefore, it is stored in the compiled bytecode if it is actually defined, otherwise it is computed. In the java specification's words:


如果类已定义serialVersionUID,则从类中检索它。如果serialVersionUID>未由类定义,则根据虚拟机中类的定义计算。如果>指定的类不可序列化或可外部化,则返回null。

If the class has defined serialVersionUID it is retrieved from the class. If the serialVersionUID is >not defined by the class, it is computed from the definition of the class in the virtual machine. If >the specified class is not serializable or externalizable, null is returned.

流唯一标识符部分,解释了此类计算的算法。

In the Stream Unique Identifiers section, the algorithm for such computation is explained.

这一段值得注意(这就是为什么当实现Serializable的类没有明确定义serialVersionUID时IDE通常会显示警告。)

This paragraph is noteworthy (that's why IDEs usually show a warning when a class implementing Serializable has not explicitly defined a serialVersionUID).

注意 :强烈建议所有可序列化的类显式声明serialVersionUID值,因为默认的 serialVersionUID计算对类详细信息非常敏感可能因编译器实现 而异,因此在反序列化期间可能会导致意外的serialVersionUID冲突,从而导致反序列化失败。

Note: 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 serialVersionUID conflicts during deserialization, causing deserialization to fail.

这篇关于Java中的序列化版本uid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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