在Java中扩展Serializable类时,我选择serialVersionUID是否重要? [英] Does it matter what I choose for serialVersionUID when extending Serializable classes in Java?

查看:263
本文介绍了在Java中扩展Serializable类时,我选择serialVersionUID是否重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扩展一个实现可序列化的界面。 Sun的文档(和我的IDE)告诉我,我应该设置这个值以防止恶作剧:

I'm extending a class (ArrayBlockingQueue) that implements the Serializable interface. Sun's documentation (and my IDE) advises me that I should set this value in order to prevent mischief:


但强烈建议所有可序列化的类都显式声明serialVersionUID值,因为默认的serialVersionUID计算对类细节高度敏感,这些细节可能因编译器实现而异,因此在反序列化期间可能会导致意外的InvalidClassExceptions。

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.

现在,我不在乎放在那里的价值。这有关系吗?

Now, I couldn't care less about what value to put in there. Does it matter?

推荐答案

否 - 只要你在合适的时间改变它(即当你做出改变影响时)序列化,例如删除一个字段)你使用什么值无关紧要。

No - so long as you change it at the right time (i.e. when you make a change which affects serialization, e.g. removing a field) it shouldn't matter what value you use.

为简单起见,我建议从0开始,每次需要时增加1 to。

For simplicity I'd suggest starting with 0 and increasing it by 1 each time you need to.

序列化规范有更多详细信息。

这篇关于在Java中扩展Serializable类时,我选择serialVersionUID是否重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆