这是什么意思:可序列化的类没有声明静态的最终 serialVersionUID 字段? [英] What does it mean: The serializable class does not declare a static final serialVersionUID field?

查看:26
本文介绍了这是什么意思:可序列化的类没有声明静态的最终 serialVersionUID 字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了标题中给出的警告信息.我想了解并删除它.我已经在这个问题上找到了一些答案,但由于技术术语过多,我不明白这些答案.能不能用简单的话来解释这个问题?

I have the warning message given in the title. I would like to understand and remove it. I found already some answers on this question but I do not understand these answers because of an overload with technical terms. Is it possible to explain this issue with simple words?

附言我知道 OOP 是什么.我知道什么是对象、类、方法、字段和实例化.

P.S. I know what OOP is. I know what is object, class, method, field and instantiation.

P.P.S.如果有人需要我的代码,它在这里:

P.P.S. If somebody needs my code it is here:

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:

序列化运行时为每个可序列化类关联一个版本号,称为 serialVersionUID,在反序列化期间使用它来验证序列化对象的发送者和接收者是否已为该对象加载了类与序列化兼容.如果接收者为对象加载了一个类,该对象的serialVersionUID 与相应发送者的类的serialVersionUID 不同,那么反序列化将导致InvalidClassException.一个可序列化的类可以通过声明一个名为 "serialVersionUID" 的字段来显式声明它自己的 serialVersionUID,该字段必须是静态的、最终的和 long 类型:

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 配置为:

You can configure your IDE to:

  • 忽略这一点,而不是发出警告.
  • 自动生成一个id

根据您的附加问题讨论的警告消息是否是我的 GUI 应用程序冻结的原因?":

As per your additional question "Can it be that the discussed warning message is a reason why my GUI application freeze?":

不,不可能.仅当您序列化对象并在不同的位置(或时间)(其中(何时)类发生更改时)反序列化它们时,它才会导致问题,并且不会导致冻结,而是在 InvalidClassException 中.

No, it can't be. It can cause a problem only if you are serializing objects and deserializing them in a different place (or time) where (when) the class has changed, and it will not result in freezing, but in InvalidClassException.

这篇关于这是什么意思:可序列化的类没有声明静态的最终 serialVersionUID 字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆