如何计算serialversionuid [英] How serialversionuid is calculated
问题描述
当我在Eclipse中创建实现 Serializable
接口的Java Class时,我收到一个警告
When I create Java Class in Eclipse which implements Serializable
interface, I get a warning
可序列化类ABCD不声明一个静态final
serialVersionUID字段类型long
The serializable class ABCD does not declare a static final serialVersionUID field of type long
所以当我点击在警告中,我在Eclipse中获取了一个选项
So when I click on warning, I get a option in Eclipse to
添加生成的串行版本ID
Add generated Serial Version ID
一旦我选择了该选项,Eclipse会为我自动创建一个 serialVersionUID
变量。
Once I choose that option, Eclipse automatically creates a serialVersionUID
variable for me.
现在我想知道在什么基础上生成这个数字。这是一个随机数?我可以提供我自己的随机数吗?
Now I wanted to know that on what basis that number is generated. It is a just any random number ? Can I provide any random number of my own ?
推荐答案
它是根据你的类的结构 - 字段,方法,等等。它在对象序列化规范 - 参阅本节
It is calculated based on the structure of your class - fields, methods, etc. It is specified in the Object Serialization Specification - see this section for the exact format.
该规范描述了没有提供任何值的情况,但自动生成使用相同的算法。
The spec describes what happens in no value is provided, but the autogeneration uses the same algorithm.
这篇关于如何计算serialversionuid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!