237

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?

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

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();
                }
            });
        }

}
Roman
  • 97,757
  • 149
  • 317
  • 426

5 Answers5

165

From the javadoc:

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:

You can configure your IDE to:

  • ignore this, instead of giving a warning.
  • autogenerate an id

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

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.

kkishere
  • 154
  • 2
  • 13
Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
  • 13
    You can also let your IDE to autogenerate one. – BalusC Feb 18 '10 at 14:08
  • 4
    @BalusC - how can we do that in eclipse ? Is it safe to auto generate ? – Jedi Knight Mar 17 '13 at 07:09
  • 4
    @JediKnight In Eclispse it is Window>Preferences>Java>Code Style>Clean Up>Missing Code>activate "Add serial version ID" under "Potential programming problems". Then if you are in the default profile, obviously you must save it as a different one. – 0x41414141 Apr 20 '14 at 09:47
  • 4
    Don't auto generate `serialVersionUID` when you are not using serialization. This already happens during compilation, without having to poison your source code with a stupid random number just to keep your IDE happy. Just turn off the Eclipse warning, because that's where the actual confusion comes from. If you ever do want to use serialization (likelyhood of that is approaching 0 these days), then read up about it, and use the `serialVersionUID` as a VERSION NUMBER (starting at 1, and incremented when your class definition changes with special code to read older versions). – john16384 Jun 15 '19 at 22:48
42

The other answers so far have a lot of technical information. I will try to answer, as requested, in simple terms.

Serialization is what you do to an instance of an object if you want to dump it to a raw buffer, save it to disk, transport it in a binary stream (e.g., sending an object over a network socket), or otherwise create a serialized binary representation of an object. (For more info on serialization see Java Serialization on Wikipedia).

If you have no intention of serializing your class, you can add the annotation just above your class @SuppressWarnings("serial").

If you are going to serialize, then you have a host of things to worry about all centered around the proper use of UUID. Basically, the UUID is a way to "version" an object you would serialize so that whatever process is de-serializing knows that it's de-serializing properly. I would look at Ensure proper version control for serialized objects for more information.

MrMas
  • 1,013
  • 2
  • 14
  • 26
  • There are several *major* mistakes in your citation, and also some self-contradictions. – user207421 Jun 14 '14 at 09:54
  • 13
    @EJP It would be helpful for all if you enumerated the errors. – MrMas Jun 18 '14 at 16:18
  • @EJP It just occurred to me that there are two citations in my answer. To which citation are you referring? – MrMas May 31 '16 at 17:55
  • 1
    I am referring to the Javaworld article, but the only citations in your answer are to third-party resources, which don't have any more standing than answers on this website: often considerably less so. The only *relevant* citations are the normative references: the JLS, the JVM Specification, the Java Object Serializatikn Specification, and the Javadoc. – user207421 May 22 '17 at 05:12
37

The reasons for warning are documented here, and the simple fixes are to turn off the warning or put the following declaration in your code to supply the version UID. The actual value is not relevant, start with 999 if you like, but changing it when you make incompatible changes to the class is.

public class HelloWorldSwing extends JFrame {

        JTextArea m_resultArea = new JTextArea(6, 30);
        private static final long serialVersionUID = 1L;
Community
  • 1
  • 1
Robin
  • 23,156
  • 4
  • 47
  • 57
  • 2
    The reasons for the warning are documented in the Java Object Serialization Specification and the [Javadoc](http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html). StackOverflow answers are not normative references. – user207421 May 22 '17 at 05:14
27

it must be changed whenever anything changes that affects the serialization (additional fields, removed fields, change of field order, ...)

That's not correct, and you will be unable to cite an authoriitative source for that claim. It should be changed whenever you make a change that is incompatible under the rules given in the Versioning of Serializable Objects section of the Object Serialization Specification, which specifically does not include additional fields or change of field order, and when you haven't provided readObject(), writeObject(), and/or readResolve() or /writeReplace() methods and/or a serializableFields declaration that could cope with the change.

user207421
  • 289,834
  • 37
  • 266
  • 440
5

Any class that can be serialized (i.e. implements Serializable) should declare that UID and it must be changed whenever anything changes that affects the serialization (additional fields, removed fields, change of field order, ...). The field's value is checked during deserialization and if the value of the serialized object does not equal the value of the class in the current VM, an exception is thrown.

Note that this value is special in that it is serialized with the object even though it is static, for the reasons described above.

Thomas Lötzer
  • 22,522
  • 16
  • 64
  • 55
  • 2
    I JFrame a serializable class? What is UID and how I can declare it? Does my code "changes something that affects the serialization"? What actually the serialization means? – Roman Feb 18 '10 at 14:00
  • Yes, JFrame is a java.awt.Component which implements Serializable. Code never changes anything that affects serialization, only programmers do that. I don't know a list that lists all changes that affect serialization. Check http://en.wikipedia.org/wiki/Serialization#Java for a description of serialization in Java. – Thomas Lötzer Feb 18 '10 at 14:12
  • 2
    It is not serialized with the object. It shouldn't be changed unless you want to break compatibility, or if you have already done so by departing from what it says in the Versioning section of the specification. That does not include adding or reordering fields. The correct reference is not Wikipedia but the Object Serialization Specification. – user207421 May 16 '11 at 01:18
  • "additional fields, removed fields, change of field order" don't matter at all. Only type changes matter if you don't implement readObject() to handle the change. – The incredible Jan Apr 23 '20 at 11:55