15

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!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Surender Thakran
  • 664
  • 3
  • 7
  • 21
  • 1
    possible duplicate of [What is a serialVersionUID and why should I use it?](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it) – Andrew Thompson Jun 16 '12 at 14:01

2 Answers2

31

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:

Preferences -> Java -> Compiler -> Errors/Warnings tab -> Potential programming problems section -> Serializable class without serialVersionUID -> Ignore

To disable the warning for a single project:

Right-click on the project -> Properties -> Java Compiler -> Errors/Warnings -> click Enable project specific settings (if necessary) -> Potential programming problems section -> Serializable class without serialVersionUID -> Ignore

Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42
Zagrev
  • 2,000
  • 11
  • 8
5

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...
Poindexter
  • 2,396
  • 16
  • 19
  • but i have worked on serialization in tha past and never declared any such field and still never got this warning. What is different this time? or what was different earlier? – Surender Thakran Jun 15 '12 at 14:33
  • also the javadoc says : `If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification.` – Surender Thakran Jun 15 '12 at 14:35
  • 1
    It's possible that Eclipse had that warning disabled. – Poindexter Jun 15 '12 at 14:35
  • Eclipse has a 'quick fix' option on that warning to generate a serialVersionUID field if you want. Or if you aren't planning on actually serializing this object, then you can just disable the compiler warninger in your Eclipse preferences. – wolfcastle Jun 15 '12 at 14:44
  • @SurenderThakran If you have worked on serialization in the past, you should know why the serial version is important in RMI/CORBA. If not, try reading up on Joshua Block's Effective Java. If you change your exception to store different types internally, for instance, then clients that interface with your code might suddenly crash as the serial version has changed. See https://stackoverflow.com/a/285809/200987 – oligofren Oct 26 '18 at 13:25