问题描述
当类在 Eclipse 中实现 Serializable 时,我有两个选择:添加默认的 serialVersionUID(1L)
或生成的 serialVersionUID(3567653491060394677L)
.我认为第一个更酷,但很多时候我看到人们使用第二个选项.是否有任何理由生成long serialVersionUID
?
When class implements Serializable in Eclipse, I have two options: add default serialVersionUID(1L)
or generated serialVersionUID(3567653491060394677L)
. I think that first one is cooler, but many times I saw people using the second option. Is there any reason to generate long serialVersionUID
?
推荐答案
据我所知,这只是为了与以前的版本兼容.这仅在您之前忽略使用 serialVersionUID 时才有用,然后进行了您知道应该是 兼容 但会导致序列化中断.
As far as I can tell, that would be only for compatibility with previous releases. This would only be useful if you neglected to use a serialVersionUID before, and then made a change that you know should be compatible but which causes serialization to break.
请参阅 Java 序列化规范更多详情.
这篇关于为什么生成长的 serialVersionUID 而不是简单的 1L?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!