`
condor_mk7
  • 浏览: 43505 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

does not declare a static final serialVersionUID

阅读更多
关键字: serialversionuid, static final serialversionuid
The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

今天在编写一个计算器的程序的时候,发现这个警告提示,于是google。得到答案如下:

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class–serialVersionUID fields are not useful as inherited members.


以上是Serializable在javadoc中的描述。

也既是说:

serialVersionUID用来作为Java对象序列化中的版本标示之用;
如果一个序列化类没有声明这样一个static final的产量,JVM会根据各种参数为这个类计算一个;
对于同样一个类,不同版本的JDK可能会得出不同的serivalVersionUID;


The serializable class XXX does not declare a static final serialVersionUID field of type long
serialVersionUID作用:
序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。

你可以随便写一个,在Eclipse中它替你生成一个,有两种生成方式:
一个是默认的1L,比如:private static final long serialVersionUID = 1L;
一个是根据类名、接口名、成员方法及属性等来生成一个64位的哈希字段,比如:private static final long serialVersionUID = -8940196742313994740L;之类的。

当你一个类实现了Serializable接口,如果没有定义serialVersionUID,Eclipse会提供这个提示功能告诉你去定义之。
在Eclipse中点击类中warning的图标一下,Eclipse就会自动给定两种生成的方式,如上面所述。如果不想定义它,在Eclipse的设置中也可以把它关掉的,设置如下:
Window ==> Preferences ==> Java ==> 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,这个可以避开不兼容性的问题。


最后,不考虑兼容性的问题,我把这个warning改成ignore了。
分享到:
评论

相关推荐

    C语言或者C++:2D and 3D arrays

    Define a two-dimensional long integer array A of 3 rows and 4 columns. Fill the elements of the arrays ...Declare a 3D array C, big enough to store two copies of array A and copy array A into C twice.

    java实验4.3(SalesArray.java )

    1. Declare a two-dimensional integer array named sales. Populate the first four columns using the following data. Quarter 1 Quarter 2 Quarter3 Quarter 4 Total Dept. 1 750 660 910 800 Dept. 2 800 700...

    apr-1.lib;apriconv-1.lib;aprutil-1.lib;libcurl.lib;mxml1.lib

    apr 1.6.5 设置宏APR_DECLARE_STATIC apriconv 1.2.2 API_DECLARE_STATIC aprutil 1.6.1 APU_DECLARE_STATIC libcurl 7.62 CURL_STATICLIB mxml github master 分支 2018-11-28 全部是mt lib ,工具 VS2015

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Do not forget that a -inl.h file requires a #define guard just like any other header file. Function Parameter Ordering link ▶When defining a function, parameter order is: inputs, then outputs. ...

    qt自定义类型 Q_DECLARE_METATYPE与qRegisterMetaType

    如果要使自定义类型或其他非QMetaType内置类型在QVaiant中使用,必须使用该宏Q_DECLARE_METATYPE。 该类型必须有公有的 构造、析构、复制构造 函数 qRegisterMetaType 必须使用该函数的两种情况:1、如果非QMetaType...

    局域网内的消息发送器Net Send

    简单的局域网内消息发送器.

    2009 达内Unix学习笔记

    [] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件。 $ 取环境变量的值。 | 管道。把前一命令的输出作为后一命令的输入,把几个命令连接起来。 |经常跟tee连用,tee 把内容保存到文档并显示出来。 三、通用后...

    declare:明确声明Javascript函数中的参数类型

    - this is not a sum, it's a string concatenation Declare.js允许您显式声明参数的类型。 仅当参数具有正确的类型时,才调用您的函数。 让我们使用Declare.js重写示例代码: var sum = declare ( [ Number , ...

    自定义控件控件的declare-styleable配置

    最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行配置,于是便查询了解了下declare-styleable,下面我把自己的使用感受和如何使用进行说明下。 declare-styleable:declare-...

    DECLARE SQLCODE INT DEFAULT 0.pdf

    DECLARE SQLCODE INT DEFAULT 0.pdf

    Android Usb Port Forwarding

    The Android architecture does not allow to start communication from the Android to the host through the USB cable.The opposite is possible, using the Google "Android Debug Bridge" (ADB in short)....

    JSP Simple Examples

    We does not make a object of the abstract class. This class must be inherited. Unlike interface the abstract class may implement some of the methods defined in the class, but in this class at least ...

    Q_DECLARE_METATYPE_qRegisterMetaType.rar

    Qt信号槽使用结构体作为参数 https://blog.csdn.net/libaineu2004/article/details/105332464

    function_declare.h

    function_declare.h

    python静态方法实例

    本文实例讲述了python静态方法。分享给大家供大家参考。 具体实现方法如下: ... A static method does not receive an implicit first argument.  To declare a static method, use this idiom:    c

    Trinity: A Distributed Graph Engine on a Memory Cloud

    Furthermore, Trinity provides a high level specification language called TSL for users to declare data schema and communication protocols, which brings great ease-of-use for general purpose graph ...

    Linux declare命令用法详解

    Linux declare命令 Linux declare命令用于声明 shell 变量。 declare为shell指令,在第一种语法中可用来声明变量并设置变量的属性([rix]即为变量的属性),在第二种语法中可用来显示shell函数。若不加上任何参数,则...

    C# Game Programming Cookbook for Unity 3D - 2014

    4.3.1 A Simple Spawn Controller..................................49 4.3.1.1 Script Breakdown................................52 4.3.2 Trigger Spawner...........................................56 4.3.3 ...

    Exporter-Declare:声明式函数导出

    名称出口商::宣告-出口正确完成描述Exporter :: Declare是一个元驱动的导出工具。 Exporter :: Declare尝试采用其他导出工具的所有良好功能,同时丢掉了可怕的界面。 Exporter :: Declare还提供了挂钩,使您可以添加...

    mysql存储过程之游标(DECLARE)原理与用法详解

    本文实例讲述了mysql存储过程之游标(DECLARE)原理与用法。分享给大家供大家参考,具体如下: 我们在处理存储过程中的结果集时,可以使用游标,因为游标允许我们迭代查询返回的一组行,并相应地处理每行。mysql的...

Global site tag (gtag.js) - Google Analytics