serialVersionUID field warning in eclipse

随声附和 提交于 2019-12-10 02:08:47

问题


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:

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

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!

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!


回答1:


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.

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

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




回答2:


TestGUI extends Frame which in turn implements Serializable. A requirement of the Serializable interface is to have a final long serialVersionUID field. See the Serializable javadoc for more info.

To quote the important part of that 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...


来源:https://stackoverflow.com/questions/11052627/serialversionuid-field-warning-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!