关于java:它是什么意思:serializable类没有声明一个静态的最终serialVersionUID字段?

What does it mean: The serializable class does not declare a static final serialVersionUID field?

本问题已经有最佳答案,请猛点这里访问。

标题中有警告信息。我想理解并删除它。我已经找到了一些关于这个问题的答案,但是我不理解这些答案,因为技术术语太多了。能用简单的词来解释这个问题吗?

另外,我知道OOP是什么。我知道什么是对象、类、方法、字段和实例化。

P.P.S.如果有人需要我的代码,请点击这里:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.awt.*;
import javax.swing.*;


public class HelloWorldSwing extends JFrame {

        JTextArea m_resultArea = new JTextArea(6, 30);

        //====================================================== constructor
        public HelloWorldSwing() {
            //... Set initial text, scrolling, and border.
            m_resultArea.setText("Enter more text to see scrollbars");
            JScrollPane scrollingArea = new JScrollPane(m_resultArea);
            scrollingArea.setBorder(BorderFactory.createEmptyBorder(10,5,10,5));

            // Get the content pane, set layout, add to center
            Container content = this.getContentPane();
            content.setLayout(new BorderLayout());
            content.add(scrollingArea, BorderLayout.CENTER);
            this.pack();
        }

        public static void createAndViewJFrame() {
            JFrame win = new HelloWorldSwing();
            win.setTitle("TextAreaDemo");
            win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            win.setVisible(true);
        }

        //============================================================= main
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    createAndViewJFrame();
                }
            });
        }

}


javadoc:from theP></

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 an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

你可以你的IDE to configure:P></

  • ignore instead of this,给警告。
  • 安自动生成ID

对你的问题"(Additional as that the warning message它discussed is a reason GUI应用冻为什么我?":P></

恩,不,我不能。它可以造成问题如果你是唯一的序列化和反序列化的对象,在不同的地方在他们(或时间)在(when)has changed the class will not result,恩在和冷冻,但在InvalidClassException。P></


原因是documented here for the warning and the fixes无知,或是把off the warning put the following the宣言,在你的代码版本的UID到电源。the actual value is not start with;999,如果你喜欢它,但当你让不兼容的变化changes to the class is。P></

1
2
3
4
public class HelloWorldSwing extends JFrame {

        JTextArea m_resultArea = new JTextArea(6, 30);
        private static final long serialVersionUID = 1L;


the other of answers我有很多技术的信息。我会尽量回答,as requested,简单的条款。P></

序列化是你把安安审对象(如果你想它在RAW转储缓冲硬盘,保存它,它在运输流(例如,在二进制对象发送过安在插座,或其他网络)创建的对象序列化binary representation of an。(for黑莓Java序列化序列化的信息在线在线看维基百科)。P></

如果你不打算要序列化的类杂志,You can add the above你只是@SuppressWarnings("serial")舱位置。P></

如果你是,那么你会有一个序列化,of things to host the proper担心在中心周围使用的UUID。基本上,"is the UUID的方式对象的序列化版本"我知道,你也知道,任何process is that是反序列化反序列化properly。我看的版本控制的对象序列化的保证proper for for黑莓信息。P></


it must be changed whenever anything
changes that affects the serialization
(additional fields, removed fields,
change of field order, ...)

这是不正确的,你unable will be for that to an authoriitative claim开源标准。当你让它should be changed a change that is the rules given in the在不兼容的版本序列化的对象和对象序列化规范section of the which does not专门开发,包括额外的域或change of场秩序,当你还没有提供readObject(), writeObject(),和/或readResolve()/writeReplace()ETF方法和/或serializableFields宣言,可以应付with the change。P></


that can be any序列化的类(即Serializableimplements)should that must be宣布UID和当事affects changes that changed the序列化(额外的字段,字段的顺序的相变场removed,of,…)。checked value is the场在反序列化和序列化对象if the value of the does not equal the value of the class在VM is the current,安例外扔了。P></

这个值是已知的,特别是在与对象序列化它,即使它是静态的,for the原因上面描述的程序办理。P></