What means 1L serialVersionUID? When could I use this default value 1L?

前端 未结 8 1985
生来不讨喜
生来不讨喜 2021-02-05 07:02

There are 3 ways to define the serialVersionUID :

1. private static final long serialVersionUID = 1L; (Default)
2. private static final long serialVersionUID = -         


        
8条回答
  •  不思量自难忘°
    2021-02-05 07:45

    The JVM will throw a InvalidClassException if the serialVersionUID of a serialized object does not match the serialVersionUID of the class it is being deserialized as.

    The serialVersionUID of a class should change every time the class changes in an incompatible way. Normally this means every time you change the shape of a class (ie fields or methods change).

    There are some cases where you don't want the serialVersionUID to change. For instance you might accept old versions of the object into your application. In this case, you can leave the serialVersionUID the same and new fields will come through as null.

提交回复
热议问题