Eclipse中出现“Add a serial version ID”的warning 解决办法

转载: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 -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中出现“Add a serial version ID”的warning 解决办法)