gpt4 book ai didi

java - serialversionuid 是如何计算的

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:05:20 26 4
gpt4 key购买 nike

当我在 Eclipse 中创建实现 Serializable 接口(interface)的 Java 类时,我收到警告

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

所以当我点击警告时,我在 Eclipse 中得到一个选项来

Add generated Serial Version ID

一旦我选择了该选项,Eclipse 就会自动为我创建一个 serialVersionUID 变量。

现在我想知道生成该数字的依据是什么。这是一个任意随机数?我可以提供我自己的任意随机数吗?

最佳答案

它是根据你的类的结构计算的——字段、方法等。它在 Object Serialization Specification 中指定。 - see this section对于确切的格式。

规范描述了在没有提供值的情况下会发生什么,但自动生成使用相同的算法。

The sequence of items in the stream is as follows:

  • The class name.
  • The class modifiers written as a 32-bit integer.
  • The name of each interface sorted by name.
  • For each field of the class sorted by field name (except private static and private transient fields:
    • The name of the field.
    • The modifiers of the field written as a 32-bit integer.
    • The descriptorof the field.
  • If a class initializer exists, write out the following:
    • The name of the method, .
    • The modifier of the method, java.lang.reflect.Modifier.STATIC, written as a 32-bit integer.
    • The descriptor of the method, ()V.
  • For each non-private constructor sorted by method name and signature:
    • The name of the method, .
    • The modifiers of the method written as a 32-bit integer.
    • The descriptor of the method.
  • For each non-private method sorted by method name and signature:
    • The name of the method.
    • The modifiers of the method written as a 32-bit integer.
    • The descriptor of the method.

关于java - serialversionuid 是如何计算的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7738162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com