“声明静态最终serialVersionUID”是什么?警告意味着如何解决? [英] What does the "declare a static final serialVersionUID" warning mean and how to fix?
问题描述
可能重复:
这是什么意思:可序列化的类没有声明一个静态的最终serialVersionUID字段?
Java编译器警告:可序列化类[*****]不声明静态最终的serialVersionUID类型为long。
为什么?如何解决?
这个解释得相当好这里:
serialVersionUID是Serializable类的通用版本标识符。反序列化使用此数字来确保加载的类与序列化对象完全对应。如果未找到匹配项,则抛出InvalidClassException。
您可以通过添加
$来修复错误b$ b
private static final long serialVersionUID = 7526472295622776147L; //唯一ID
到班级。
进一步阅读:
附注:如果你正在使用Eclipse,如果你(而且没有其他人)曾经计划过序列化您的课程,您也可以通过转到
窗口→首选项→Java→编译器→错误/警告<来抑制错误/ p>
并在Serializable Class without serialVersionUID上选择忽略。
Possible Duplicate:
What does it mean: The serializable class does not declare a static final serialVersionUID field?
Java compiler warning: The serializable class [*****] does not declare a static final serialVersionUID field of type long.
Why? How to fix it?
This is explained fairly well here:
The serialVersionUID is a universal version identifier for a Serializable class. Deserialization uses this number to ensure that a loaded class corresponds exactly to a serialized object. If no match is found, then an InvalidClassException is thrown.
You fix the error by adding
private static final long serialVersionUID = 7526472295622776147L; // unique id
to the class.
Further reading:
- java.io.Serializable
- Why should I bother about serialVersionUID? (stackoverflow)
A side note: If you're using Eclipse and if you (and no one else) ever plan to serialize your classes, you can also suppress the error by going to
Window → Preferences → Java → Compiler → Errors/Warnings
and select "Ignore" on "Serializable Class without serialVersionUID".
这篇关于“声明静态最终serialVersionUID”是什么?警告意味着如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!