问题描述
我可能会冒这个问题冒险。
I am probably risking some downvotes on this.
在我看来,为新类明确指定serialVersionUID是不好的。考虑两种情况,即在布局应该更改时不更改它,并在不应该更改时更改它。
It seems to me that explicitly specifying serialVersionUID for new classes is bad. Consider the two cases of not changing it when layout has it should have been changed and changing it when it should not have.
几乎只有当它是明确的时才发生变化。在这种情况下,它会导致一些非常微妙,难以发现的错误。特别是在开发过程中,类布局经常变化。但如果没有明确指定,它将会改变并且反序列化将大声破坏,通常可能通过清除存储库来解决。
Not changing when it should have been changed occurs almost only when it is explicit. In this case, it results in some very subtle, hard-to-find bugs. Especially during development, when class layout changes often. But if it has not been explicitly specified, it will change and the deserialization will break loudly, mostly likely solved by purging the repository.
在不应该发生时更改它几乎只有在隐含的时候。这是一种罕见的情况,其中类布局已更改但我们仍希望从旧的序列化blob反序列化。这可能会在QA期间被捕获(从5.2升级到5.2.1之后的奇怪错误,请参阅附加的堆栈跟踪),并且可以通过设置显式值来轻松修复。
Changing it when should not have would occur almost only when it is implicit. This is the rare case where class layout has changed but we still want to deserialize from the old serialized blobs. This will likely be caught during QA (Strange errors after upgrade from 5.2 to 5.2.1, see attached stack trace) and can be trivially fixed by setting a explicit value.
评论?
推荐答案
改变何时不应该发生由于类布局更改以外的原因 - 问题在于它依赖于编译器实现。如果您使用Eclipse进行调试但使用javac进行生产构建,则可能最终会出现两个不兼容的数据集。
Changing when it shouldn't may happen for reasons other than class layout changes - the problem is that it's compiler implementation dependent. If you do debug with Eclipse but do production builds with javac, you may end up with two incompatible sets of data.
这篇关于显式serialVersionUID被认为有害吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!