java eclipse中的serialVersionUID字段警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11052627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:36:11  来源:igfitidea点击:

serialVersionUID field warning in eclipse

javaserializationawtserialversionuid

提问by Surender Thakran

I have just started on AWT and made a simple program in it, it works fine but it shows a warning message in eclipse which i don't understand:

我刚刚开始使用 AWT 并在其中制作了一个简单的程序,它运行良好,但它在 eclipse 中显示了一条我不明白的警告消息:

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

可序列化类 TestGUI 未声明 long 类型的静态最终 serialVersionUID 字段

I know that the warning message is not related to AWT and there was no need to post my whole code but when i tried to make a SSCCE of the code the warning also disappeared. Since I don't know why this warning is generated i didn't knew which part to retain in my SSCCE. Hence the whole code!

我知道警告消息与 AWT 无关,并且没有必要发布我的整个代码,但是当我尝试制作代码的 SSCCE 时,警告也消失了。因为我不知道为什么会产生这个警告,所以我不知道在我的 SSCCE 中保留哪个部分。因此整个代码!

My code is:

我的代码是:

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestGUI extends Frame {
    /**
     * @param args
     */
    private int x = 50;
    private int y = 50;

    TestGUI(String s) {
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                setVisible(false);
                System.exit(0);
            }
        });
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent me) {
                x = me.getX();
                y = me.getY();
                repaint();
            }
        });
    }

    public void paint(Graphics g) {
        g.drawString("Hello Princess", 100, 100);
        g.drawString("Mouse clicked here", x, y);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGUI tg = new TestGUI("first");
        tg.setSize(500, 500);
        tg.setVisible(true);
    }

}

Thanx in advance!

提前谢谢!

回答by Zagrev

Eclipse used to have that warning disabled by default. In Eclipse Indigo (3.7), the default was to enable the warning. You can change the setting in 2 places, one for everything in the workspace, and one for a single project.

Eclipse 曾经默认禁用该警告。在 Eclipse Indigo (3.7) 中,默认设置是启用警告。您可以在 2 个位置更改设置,一个用于工作区中的所有内容,另一个用于单个项目。

To disable the warning for all projects in the workspace, go to Window / Preferences and open the Java / Compiler / "Errors/Warnings" tab, and open "Potential programming problems", then change the value of "Serializable class without serialVersionUID" to Ignore (or whatever you think is appropriate).

要禁用工作区中所有项目的警告,请转到窗口/首选项并打开 Java/编译器/“错误/警告”选项卡,然后打开“潜在的编程问题”,然后将“没有 serialVersionUID 的可序列化类”的值更改为忽略(或任何你认为合适的)。

To disable the warning for a single project, you can right-click on the project, select Properties, then go to Java Compiler / "Errors/Warnings", click Enable project specific settings (if necessary), then select "Potential programming problems" and change the value of "Serializable class without serialVersionUID" to Ignore (or whatever you think is appropriate).

要禁用单个项目的警告,您可以右键单击项目,选择属性,然后转到 Java 编译器/“错误/警告”,单击启用项目特定设置(如有必要),然后选择“潜在的编程问题”并将“没有serialVersionUID的可序列化类”的值更改为忽略(或您认为合适的任何值)。

回答by Poindexter

TestGUIextends Framewhich in turn implements Serializable. A requirement of the Serializableinterface is to have a final long serialVersionUIDfield. See the Serializable javadocfor more info.

TestGUIextendsFrame依次实现Serializable. Serializable接口的一个要求是有一个final long serialVersionUID字段。有关更多信息,请参阅Serializable javadoc

To quote the important part of that Javadoc:

引用该 Javadoc 的重要部分:

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