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

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

问题描述

我正在扩展一个类 (ArrayBlockingQueue) 来实现 可序列化的接口.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.

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

序列化规范 有更多细节.

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

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