开发者

serialVersionUID no longer required from Java 5 onwards?

开发者 https://www.devze.com 2023-04-04 17:54 出处:网络
I recently read a comment saying that usage of serialVersionUID to make different versions of the same class 开发者_运维知识库compatible for serialization/deserialization is no longer needed from Java

I recently read a comment saying that usage of serialVersionUID to make different versions of the same class 开发者_运维知识库compatible for serialization/deserialization is no longer needed from Java 5 onwards. Is this correct? Maybe it got mixed-up with covariant return types. So wanted to check it out with you all.


The JavaDocs for Java 5 still states that it is needed:

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members.


Since Java 1.4 you have been able to use XMLEncoder for Serialisation and this ignores any serialVersionUID, as does most other serialisation libraries.

However if you use ObjectOutputStream, its usage of serialVersionUID has not changed.

0

精彩评论

暂无评论...
验证码 换一张
取 消