Why serialVersionUID in java must be static, final, and of type long?
This question already has an answer here:
Because the specification says so.
No. It only differentiates between different versions of your class (eg compiled at different points in time). If you're writing a new class you can set serialVersionUID = 1L;
without any problems whatsoever.
Two or more classes can declare the same serialVersionUID. The actual wire format contains the fully-qualified class name so there is no ambiguity.
If the serialVersionUID in the data being loaded does not match the serialVersionUID of your class, an InvalidClassException
will be thrown.
It's static because it belongs to the class, not to an instance.
It's final because it doesn't change (for that version of the class). It only changes when someone makes a change to the class that affects how it is serialized.
It's a long because the data type is easy to serialize, fits in a compact space, and has many more values than anyone is likely to need.
It doesn't have to be unique. If two versions of a class have the same serialversionUID that means there are no changes to the class that impact how it's serialized, those two versions of the class can accept the same serialization format.
It doesn't matter if two classes have the same serialVersionUID, it's not used between classes, it's only used to check whether the serialized version of the class is compatible with that version of the class.
链接地址: http://www.djcxy.com/p/4084.html上一篇: python如何实现相互递归?