关于serialVersionUID的问题

序列化对象在Java中主要有两个目的,一个是钝化存储对象,另一个是通过网络传输对象。

Eclispe  写Java类时,只要这个类实现了串行化(implements java.io.Serializable)就会出现以下错误信息:

"the serializable class 'myClassName' does not declare a static final serialVersionUID field of the tupe long".

去掉这个错误提示其实很简单: Window->Preferences->Java->Compiler->Errors/Warnings->Potential programming problems->Serializable class without serialVersionUID,选择Ignore,就可以了。

或者按照eclipse的建议(add default serial Version ID),增加如下代码: private static final long serialVersionUID = 1L; 就没有错误提示了。

串行化是实现对运行期的对象的存取。但不同版本的编译器(如1.3,1.4,1.5)产生的serial version UID可能不同,所以程序可能会在以后出现运行不兼容的问题(如更换jdk版本或web容器等)。为了避免这些问题,对实现了串行化的类最好按官方的要求显式的指明serialVersionUID,加上private static final long serialVersionUID = 1L; (1L就是long类型的ID,可按实际情况更改)

http://java.sun.com/j2se/1.5.0/compatibility.html Incompatibilities in the Java 2 Platform Standard Edition 5.0 (since 1.4.2) 7. Serialization - Changes in compiler-generated synthetics affect the default serial version UID, and therefore can cause serialization incompatibility when that UID is not explicitly overridden.

http://java.sun.com/j2se/1.4/compatibility.html Incompatibilities Between Java 2 Platform, Standard Edition, v1.4.0 and v1.3 2. If a serializable nested class contains explicit references to a class object (for example, "Foo.class"), then the computed value of the default serial version UID for both the nested class and its enclosing class will be different in J2SE 1.3 and J2SE 1.4. The difference is due to the fact that the computation of the serial version UID is sensitive to modifications made in the javac compiler between J2SE 1.3 and J2SE 1.4.

To avoid this problem affecting your applications, we recommend that you add an explicit serial version UID to your serializable classes. You can use the serialver tool to obtain the serial version UID of classes compiled with the J2SE 1.3 javac compiler.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值