What does "serialVersionUID" mean?

Asked

Viewed 930 times

6

Eclipse suggested I put this snippet of code:

/**
 * 
 */
private static final long serialVersionUID = 1L;

I’d like to know what his purpose is... If anyone can explain it to me, I’d be grateful.

  • This will help you: http://blog.caelum.com.br/understanding-o-serialversionuid/

2 answers

7


What is

In practice, this number would be the version of your class. You should change it whenever you add, modify or remove an attribute from the class.

Serialization

This is used during the process of serialization and deserialization of an object/instance of that class.

Basically, serialization is the process where Java takes the value of each attribute and generates a sequence of bytes. Along with this byte sequence, goes the serialVersionUID.

On the other hand, deserialization is the opposite, that is, Java takes a sequence of bytes and places them in the attributes of a new object. Before doing so, he checks whether the serialVersionUID except is equal to the object being created.

In theory, this allows you to save a "photograph" of an object, for example, in a disk file and then restore the object with the same values later.

The class has changed, and now?

But say you save an object to a file, modify your program by modifying the type of an attribute, run the program again and try to read the file to the object.

Something very strange may occur, after all Java has no way of knowing that its class has been modified since you saved the file, unless the serialVersionUID be different.

In this case, the expected behavior is that the exception InvalidClassException is launched, which makes it easier to discover the problem, but does not solve the situation.

If you need to do custom operations during serialization or deserialization, Java allows you to implement methods respectively writeObject and readObject where you will have full control over the process. See more details on documentation.

Why the Eclipse emits a warning

For an object to be serialized it needs to be marked with the interface java.io.Serializable.

If Eclipse (or some other tool that parses code) finds a class that implements Serializable directly or indirectly, he understands that it is good practice to specify a serialVersionUID.

This can occur if the class:

  • implements Serializable;
  • implements an interface that extends Serializable; or
  • extends a class that implements Serializable

What happens if I ignore?

In case you don’t specify a serialVersionUID, but make the class implements Serializable, Java will use an automatic mechanism to generate a serialVersionUID during the compilation of the class.

The generated value is based on the class characteristics as per the Java language serialization specification. However, if you use serialization mechanisms, it is recommended to specify the serialVersionUID, because the automatic implementation of Java may vary in some way between different versions or even distributions.

Why use serialization?

The most common scenario of serialization use occurs during communication between different Java processes, for example during remote calls (RPC) or distributed web applications that share session objects, where it sometimes occurs that a user’s session is migrated from one server to another, if that user’s requests are met by different servers at different times.

Problems may arise only if the servers are running different versions of the same program, so when objects are transmitted the target server cannot "unpack" the classes received from the source server.

Due to all these problems, it is most recommended to avoid using serialization, whether to save objects to disk, remote calls or even session migrations.

In the above cases, saving objects in a more flexible and independent format, such as JSON or XML, will avoid problems with serialization. In the case of remote calls, prefer a REST API. And to avoid session migration in clusters, don’t rely on session objects, but use stateless (stateless) services whenever possible.

3

The serialVersionUID is a universal version identifier for a class Serializable. In deserialization, this number is used to ensure that a loaded class corresponds exactly to a serialized object. If no match of the object is found, then a Invalidclassexception.

Obs.: Translated and adapted from: http://www.javapractices.com/topic/TopicAction.do?Id=45

Browser other questions tagged

You are not signed in. Login or sign up in order to post.