Eclipse中出现“Add a serial version ID”的warning 解决办法
2016-02-03 16:14
465 查看
转载:http://sunshyfangtian.iteye.com/blog/635845
最近重新安装了Eclipse,在设计一个不继承任何类的类时,不会有任何warning,但在设计一个子类时,编译器经常提示“warning”,在网上查了很多资料终于搞定了这个问题:如下
测试提示:The serializable class FormatStr does not declare a static final serialVersionUID field of type long
发现在我继承一些类时,会提示要这个,我打开所继承的类,里面是这样写的:
static final long serialVersionUID = -3387516993124229948L;
这个有什么作用呢??
如果我这个子类一定要声明这个serialVersionUID 那应该怎样构造它的值呢??自己随便写一个long值就行了吗???
网上某人解答如下:
那与jdk的版本没关系吧.
那是Eclipse提供这个提示功能给你吧.
你点它warning的icon两下Eclipse就会自动给定.
如果你不喜欢,可以把它关掉,windows -> preferences -> compiler -> Error/Warnings-> Potential Programming problems
将Serializable class without serialVersionUID的warning改成ignore.
其实如果你没有考虑到兼容性问题时,那就把它关掉吧.
其实有这个功能是好的.
只要任何类别实作了Serializable这个介面,
如果没有加入serialVersionUID,Eclipse都会给你warning提示,
这个serialVersionUID为了让该类别Serializable後兼容.
考虑一下,如果今天你的类Serialized存到硬碟里,
可是後来你却更改了类别的field(增加或减少或改名).
当你Deserialize时,就会出现Exception.这样就会做成不兼容性的问题.
但当serialVersionUID相同时,它就会将不一样的field以type的预设值Deserialize.
这个可以避开不兼容性的问题.
至于你说内存的问题,我觉得没差多少吧.
在eclipse中,当一个类继承另一个类时,总是提示一个如下问题:
“The serializable class A does not declare a static final serialVersionUID field of type long”
然后我按照eclipse的建议(add default serial Version ID),增加如下代码:
private static final long serialVersionUID = 1L;就没有错误提示了。
而且如果所定义的类没有继承其它类,也没有这个问题。
SUN官方网站解说摘要:
adcworks : Hi,
I develop with Eclipse IDE and am using the latest milestone that includes Java 5 syntax.
Since having upgraded, I get hundreds of the same warning for any class that extends another class with the following message:
Serializable class X does not declare a static final serialVersionUID field of type long.
I would like to know
a) Should I really ought to be doing something properly with this field, and if so, can you advise the best way to approach that.
b) Can I turn off these warnings. They are not in the Eclipse filter to exclude.
c) Is it safe to ignore them?
Thanks!
ehodges :
> a) Should I really ought to be doing something
> properly with this field, and if so, can you advise
> the best way to approach that.
Yes, if you actually serialize those objects. It can make version maintenance easier.
> b) Can I turn off these warnings. They are not in the Eclipse filter to exclude.
You turn them off on the Preferences | Compiler | Style page. It's the last item.
> c) Is it safe to ignore them?
Sure. You just won't have as much control over serialized versions.
ehodges :
> Use the serialver.exe tool that comes with the jdk.
Or hit <ctrl>-1 in Eclipse. It will add a default or generated serial version ID for you.http://forum.java.sun.com/thread.jspa?threadID=581654&messageID=3501336
(Sun网页地址)
最近重新安装了Eclipse,在设计一个不继承任何类的类时,不会有任何warning,但在设计一个子类时,编译器经常提示“warning”,在网上查了很多资料终于搞定了这个问题:如下
测试提示:The serializable class FormatStr does not declare a static final serialVersionUID field of type long
发现在我继承一些类时,会提示要这个,我打开所继承的类,里面是这样写的:
static final long serialVersionUID = -3387516993124229948L;
这个有什么作用呢??
如果我这个子类一定要声明这个serialVersionUID 那应该怎样构造它的值呢??自己随便写一个long值就行了吗???
网上某人解答如下:
那与jdk的版本没关系吧.
那是Eclipse提供这个提示功能给你吧.
你点它warning的icon两下Eclipse就会自动给定.
如果你不喜欢,可以把它关掉,windows -> preferences -> compiler -> Error/Warnings-> Potential Programming problems
将Serializable class without serialVersionUID的warning改成ignore.
其实如果你没有考虑到兼容性问题时,那就把它关掉吧.
其实有这个功能是好的.
只要任何类别实作了Serializable这个介面,
如果没有加入serialVersionUID,Eclipse都会给你warning提示,
这个serialVersionUID为了让该类别Serializable後兼容.
考虑一下,如果今天你的类Serialized存到硬碟里,
可是後来你却更改了类别的field(增加或减少或改名).
当你Deserialize时,就会出现Exception.这样就会做成不兼容性的问题.
但当serialVersionUID相同时,它就会将不一样的field以type的预设值Deserialize.
这个可以避开不兼容性的问题.
至于你说内存的问题,我觉得没差多少吧.
在eclipse中,当一个类继承另一个类时,总是提示一个如下问题:
“The serializable class A does not declare a static final serialVersionUID field of type long”
然后我按照eclipse的建议(add default serial Version ID),增加如下代码:
private static final long serialVersionUID = 1L;就没有错误提示了。
而且如果所定义的类没有继承其它类,也没有这个问题。
SUN官方网站解说摘要:
adcworks : Hi,
I develop with Eclipse IDE and am using the latest milestone that includes Java 5 syntax.
Since having upgraded, I get hundreds of the same warning for any class that extends another class with the following message:
Serializable class X does not declare a static final serialVersionUID field of type long.
I would like to know
a) Should I really ought to be doing something properly with this field, and if so, can you advise the best way to approach that.
b) Can I turn off these warnings. They are not in the Eclipse filter to exclude.
c) Is it safe to ignore them?
Thanks!
ehodges :
> a) Should I really ought to be doing something
> properly with this field, and if so, can you advise
> the best way to approach that.
Yes, if you actually serialize those objects. It can make version maintenance easier.
> b) Can I turn off these warnings. They are not in the Eclipse filter to exclude.
You turn them off on the Preferences | Compiler | Style page. It's the last item.
> c) Is it safe to ignore them?
Sure. You just won't have as much control over serialized versions.
ehodges :
> Use the serialver.exe tool that comes with the jdk.
Or hit <ctrl>-1 in Eclipse. It will add a default or generated serial version ID for you.http://forum.java.sun.com/thread.jspa?threadID=581654&messageID=3501336
(Sun网页地址)
相关文章推荐
- JavaWeb学习总结(五十)——文件上传和下载
- Spring框架学习(一)
- java fork/join
- Java关键字static
- SpringMVC从Controller跳转到另一个Controller
- 打印近似圆&方法的理解
- ZZ指令重排序
- spring事务管理几种方式(转)
- Eclipse中SVN的安装步骤和使用方法
- STS/eclipse设置utf-8
- Java解析Xml的三种方式总结
- STS导入已有工程/STS更换工作区间/STS中加jdk的lib
- spring4.2 定时任务
- Java设计模式编程之工厂方法模式的使用
- java.lang.NoClassDefFoundError: com/google/common/reflect/Invokable
- 解决Eclipse和myeclipse在进行 html,jsp等 页面编辑时,自动格式化变丑的问题
- 使用ASM操作Java字节码,实现AOP原理
- 40个Java多线程问题小结
- java分页工具类
- 自定义异常/导包/JUnit/设置快捷键/ myeclipse自带的数据库/生成API的html文档