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

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

 1 package learn;
 2
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.awt.event.*;
 6
 7 public class SimpleGui3C implements ActionListener{
 8
 9     JFrame frame;
10
11     public static void main(String[] args) {
12         SimpleGui3C gui = new SimpleGui3C();
13         gui.go();
14     }
15
16     public void go() {
17         frame = new JFrame();
18         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19
20         JButton button = new JButton("Change color");
21         button.addActionListener(this);
22
23         MyDrawPanel drawPanel = new MyDrawPanel();
24
25         frame.getContentPane().add(BorderLayout.SOUTH, button);
26         frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
27         frame.setSize(600, 300);
28         frame.setVisible(true);
29     }
30
31     public void actionPerformed(ActionEvent event) {
32         frame.repaint();
33     }
34 }

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)

时间: 2024-10-03 23:17:47

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

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 serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 你可以随便写一个,在Eclipse中它替你生成一个,有两种生成方式:一个是默认的1L,比如:private static final long serialVersionUID = 1L;一个是根

Eclipse警告: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. 当你

private static final long serialVersionUID = 1L;详解

public class User implements Serializable { /** * serialVersionUID */ private static final long serialVersionUID = 1L; /** * 主键ID */ private Integer userId; /** * 用户名 */ private String userName; /** * 密码 */ private String passWord; /** * 姓名 */ privat

创建servlet类时出现的“private static final long serialVersionUID = 1L;”语句是什么意思啊?

实现java.io.Serializable这个接口是为序列化,serialVersionUID 用来表明实现序列化类的不同版本间的兼容性.如果你修改了此类, 要修改此值. 否则以前用老版本的类序列化的类恢复时会出错. 实现后如果你用的是工具的话会出来警告,他会提示你,可以自动生成private static final long serialVersionUID = 1L; 为了在反序列化时, 确保类版本的兼容性,最好在每个要序列化的类中加入private static final long

private static final long serialVersionUID = 1L;

作者:郭无心链接:https://www.zhihu.com/question/24852886/answer/117314768来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 其实答主想问的是serialVersionUID的作用. 简单来说,Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的. 在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionU

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

Unlike methods, a constructor cannot be abstract, static, final, native  or synchronized. 1. A constructor is not inherited, so there is no need to declare it final 2. As the same reason above, an abstract constructor could never be implemented. 3. A

困扰你的private static final long serialVersionUID

很多时候一个新手在写代码的时候,往往你的IDE就会告诉你一个警告 然后你点击处理这个警告之后,它就会默认给你的类生成一个 private static final long serialVersionUID = 1L; 然后强迫症的孩子就一直不明白为什么会需要这个东西,这个东西到底是干嘛用得呢? 然后渐渐的你会发现,这个东西和你写的代码毫无关系,没什么卵用,于是渐渐的你就把他扔在一边了. 这次我就需要来解决这个问题,困扰你的private static final long serialVers

private static final long serialVersionUID = 1L用来表明类的不同版本间的兼容性

Java中serialVersionUID的解释 serialVersionUID作用: 相当于java类的身份证.主要用于版本控制.serialVersionUID作用是序列化时保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性.有两种生成方式: 一个是默认的1L,比如:private static final long serialVersionUID = 1L; 一个是根据类名.接口名.成员方法及属性等来生成

static final常量

package oo.day06;//static final常量public class StaticFinalDemo { public static void main(String[] args) { //Aoo.NUM = 250; //编译错误,常量不能修改 //System.out.println(Aoo.NUM); //1.方法区中加载Boo.class //2.将NUM1存储在方法区中 //3.去方法区中获取NUM1的值并输出 System.out.println(Boo.NU