How serialversionuid is calculated

前端 未结 2 1034
猫巷女王i
猫巷女王i 2020-12-31 17:47

When I create Java Class in Eclipse which implements Serializable interface, I get a warning

The serializable class ABCD does not declar

相关标签:
2条回答
  • 2020-12-31 18:02

    Automatically generated serialVersionIds are a hash based on the method signatures, parameters and such like in the class. It's done this way so that the serialVersionId will change whenever you change your class, indicating to the serialization mechanism that there data/class are no longer compatible. That's the default.

    When you define your own, just start with 1, and increment when the class is no longer compatible with previously serialized data.

    0 讨论(0)
  • 2020-12-31 18:16

    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.

    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 descriptor of 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.
    0 讨论(0)
提交回复
热议问题