Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serialVersionUID field warning in eclipse

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!

like image 514
Surender Thakran Avatar asked Jun 15 '12 14:06

Surender Thakran


People also ask

What is serialVersionUID?

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.

How serialVersionUID is generated when it's not defined in code?

If class does not contain a serialVersionUID field, its serialVersionUID will be automatically generated by the compiler. Different compilers, or different versions of the same compiler, will generate potentially different values.


2 Answers

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

like image 135
Zagrev Avatar answered Oct 12 '22 02:10

Zagrev


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...
like image 43
Poindexter Avatar answered Oct 12 '22 01:10

Poindexter