Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean: The serializable class does not declare a static final serialVersionUID field? [duplicate]

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();                 }             });         }  } 
like image 348
Roman Avatar asked Feb 18 '10 13:02

Roman


People also ask

Is serializable consider declaring a serialVersionUID?

However, 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, and can thus result in unexpected InvalidClassExceptions during ...

What is the use of serialVersionUID in serialization?

Simply put, we use the serialVersionUID attribute to remember versions of a Serializable class to verify that a loaded class and the serialized object are compatible. The serialVersionUID attributes of different classes are independent. Therefore, it is not necessary for different classes to have unique values.

What is serialVersionUID in Java exception?

The serialization at 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.

How do you declare serialVersionUID?

The SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads the state of the object from a file.


1 Answers

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.

like image 113
Bozho Avatar answered Sep 30 '22 22:09

Bozho