您的位置:首页 > 产品设计 > UI/UE

关于serialVersionUID的问题

2008-07-01 16:21 357 查看
序列化对象在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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: