在编译以下Java程序时,出现The serializable class  does not declare a static final serialVersionUID field of type long警告

 package learn;

 import javax.swing.*;
import java.awt.*;
import java.awt.event.*; public class SimpleGui3C implements ActionListener{ JFrame frame; public static void main(String[] args) {
SimpleGui3C gui = new SimpleGui3C();
gui.go();
} public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Change color");
button.addActionListener(this); MyDrawPanel drawPanel = new MyDrawPanel(); frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.setSize(600, 300);
frame.setVisible(true);
} public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}
package learn;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D; import javax.swing.JPanel; public class MyDrawPanel extends JPanel{
// private static final long serialVersionUID = 6201378234876555585L; public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g; int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int ) (Math.random() * 255);
Color startColor = new Color(red, green, blue); red = (int) (Math.random() * 255);
green = (int) (Math.random() * 255);
blue = (int) (Math.random() * 255);
Color endColor = new Color(red, green, blue); GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
g2d.setPaint(gradient);
g2d.fillOval(70, 70, 100, 100);
}
}

为理解这一错误信息,我们得先搞懂几个问题:

Q: What is a serialVersionUID?

A: The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in anInvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

private static final long serialVersionUID = 6201378234876555585L;

  SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization.You must declare serialVersionUID because it give us more control.

Q:What happen if i don't want to declare a serialVersionUID

A: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.

Q:Why should I use serialVersionUID?

A:Regardless of what serialized form you choose, declare an explicit serial version UID in every serializable class you write. This eliminates the serial version UID as a potential source of incompatibility (Item 74). There is also a small performance benefit. If no serial version UID is provided, an expensive computation is required to generate one at runtime.

Q:If I wanna ignore such warning , what should i do?

A:Window > Preferences > Java > Compiler > Errors / Warnings > Potential Programming Problems (only eclipse)

The serializable class does not declare a static final serialVersionUID field of type long的更多相关文章

  1. 关于The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

    编写实体类并且继承序列化接口时候,实体类会有警告,要生成一个静态的serialVersionUID. 上网搜了一下资料,现通俗解释一下: 点击前2个选项,会生成: private static fin ...

  2. Eclipse下关于The serializable class UsersServlet does not declare a static final serialVersionUID field of type的警告

    The serializable class XXX does not declare a static final serialVersionUID field of type long seria ...

  3. The serializable class XXX does not declare a static final serialVersionUID field of type long

    问题: 在Eclipse中,继承类时,总是提示下面的警告(Warning),按理说警告是没有关系的,但是程序看起来老不爽了,这是强迫症的关系(呵呵) The serializable class XX ...

  4. Eclipse警告:The serializable class XXX does not declare a static final serialVersionUID field of type long

    serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 在Eclipse中可以自动生成,有两种生成方式: 一个是默认的1L,比如:privat ...

  5. The serializable class XXX does not declare a static final serialVersionUID field of type long的警告

    原文: http://blog.csdn.net/ultrakang/article/details/41820543

  6. the serializable class XXX does not declare a static final seriaVersionUID...的问题

    关于myeclips提示The serializable class XXX does not declare a static final serialVersionUID field of typ ...

  7. private static final long serialVersionUID = 1L;详解

    public class User implements Serializable { /** * serialVersionUID */ private static final long seri ...

  8. 为什么构造器不能是abstract, static, final, native or synchronized的?

    Unlike methods, a constructor cannot be abstract, static, final, native  or synchronized. 1. A const ...

  9. 困扰你的private static final long serialVersionUID

    很多时候一个新手在写代码的时候,往往你的IDE就会告诉你一个警告 然后你点击处理这个警告之后,它就会默认给你的类生成一个 private static final long serialVersion ...

随机推荐

  1. git 使用(二)

    之前写过一篇git使用(一),那是入门篇,现在的(二)可以说是进阶篇吧,主要讲一些使用过程的注意事件及相关问题的解决办法. 一.push和fetch还需要输入用户名和密码? 解决办法:看看公玥是否添加 ...

  2. js dom

    JavaScript的DOM操作 1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方 ...

  3. 关于c#字典key不存在的测试

    之前一直隐约记得没有创建key会报异常,测试了下. 测试结果: 写入值,如果不存在key,会自动创建. 取值,如果不存在key,会报异常. 一般用c#提供了尝试取值方法,不过有out参数,考虑写扩展 ...

  4. SPOJ 422 Transposing is Even More Fun(polay计数)

    题目链接:http://www.spoj.com/problems/TRANSP2/ 题意: 思路:不妨设a=1,b=2, 我们发现(001,010,100)组成一个置换,(011,110,101)组 ...

  5. 基于Linux的oracle数据库管理 part1( 准备及linux基础 )

    主要内容 1. 安装VMware tools (好处, 显示效果增强, 从虚拟机出来不需要alt+ctrl 切换) 2. Linux 启动过程, 方便以后oracle 自动启动与关闭 3. Linux ...

  6. Android开源库--PhotoView图片查看

    如果说我比别人看得更远些,那是因为我站在了巨人的肩上. github地址:https://github.com/chrisbanes/PhotoView 介绍 在一般的应用中,总会遇到查看图片的功能, ...

  7. 线程——委托InvokeRequired和Invoke

    C#中禁止跨线程直接访问控件,InvokeRequired是为了解决这个问题而产生的,当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它.此时它将会在内部调用ne ...

  8. Hadoop2配置详解

    配置文件 hadoop的配置是由两种重要类型的配置文件进行驱动的: 默认是只读的配置: core-default.xml, hdfs-default.xml, yarn-default.xml and ...

  9. JavaScript的一些常见误区

    原文出处: 色拉油的博客   接触JavaScript两年多遇到过各种错误,其中有一些让人防不胜防,原来对JavaScript的误会如此之深,仅以此文总结一下常见的各种想当然的误区. String r ...

  10. (转)UILabel的详细使用

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 40)];   //初始化UIlbel并设定frame lab ...