JavaRush /Java Blog /Random EN /Why use SerialVersionUID inside Serializable class in Jav...
0xFF
Level 9
Донецк

Why use SerialVersionUID inside Serializable class in Java

Published in the Random EN group
Serializationand SerialVersionUIDalways remains a mystery to many Java developers. I often see questions about what is SerialVersionUID, or what happens if I don't declare SerialVersionUIDin my Serializable-class? Why use SerialVersionUID inside Serializable class in Java - 1Apart from being confusing and infrequently used, another reason for this question is the Eclipse IDE's missing warning SerialVersionUID, for example: " The Serializable class Customer does not declare a static final SerialVersionUID fieldSerializable of Customertype SerialVersionUIDlong " long"). In this article, you will not only learn the basics of Java SerialVersionUIDbut also its impact on the process of serialization and de-serialization. When you declare a class, as Serializableby implementing a token interface java.io.Serializable, the Java runtime stores an instance of that class on disk using the default serialization mechanism unless you configure the process to use the Externalizable interface . During serialization, the Java runtime creates a version number for the class so that it can deserialize it later. In Java, this version number is known as SerialVersionUID. If, during deserialization, SerialVersionUIDthere is no match, then the process will exit with an exception InvalidClassExceptionin the " main" stream java.io.InvalidClassException, and will also print the class name and the corresponding SerialVersionUID. A quick solution to fix this problem is to copy SerialVersionUIDand define it as a type constant private static final longin your class. In this article, we will learn about why we should use SerialVersionUIDin Java and how to use the serialver JDK tool to generate this ID. If you are new to serialization, you can also watch Top 10 Java Serialization Interview Questions to assess your knowledge and find gaps in your understanding for further reading. Like Concurrency(concurrency) and Multi-threading(multithreading), Serialization(serialization) is another topic that deserves reading several times.

Why use SerialVersionUID in Java

As I said, when we haven't defined a value SerialVersionUIDas static final longin our class, the serialization mechanism will do it for us. This mechanism is sensitive to many details, including the fields of your class, their access modifiers, the interfaces it implements, and even different compiler implementations; any changes to the class or use of a different compiler may produce a different result, SerialVersionUIDwhich will ultimately stop the serialized data from being reloaded. It's risky to rely on Java's serialization mechanism to generate this id, which is why it's a good idea to explicitly define it SerialVersionUIDin your Serializable class . I highly recommend reading the Java classic - Joshua Bloch “Effective Java” to understand Java serialization and the problems of handling them incorrectly. By the way, the JDK also provides a tool serialver, located in the bin directory of the JAVA_HOME directory , on my computer, C:\Program Files\Java\jdk1.6.0_26\bin\serialver.exe , which can be used to generate SerialVersionUIDfor old classes. This is very useful in case you have made changes to your class that breaks serialization and your application is unable to reload serialized instances. You can easily use this utility to create SerialVersionUIDold instances, and then use it explicitly by declaring the field as private static final long SerialVersionUID . By the way, it is highly recommended for performance and security reasons to use the regular binary format for serialization; again, “Effective Java” has several paragraphs that demonstrate the advantages of the regular format in great detail.

How to use serialver JDK utility to generate SerialVersionUID

You can use serialverto generate SerialVersionUIDfor classes. This is especially useful for developing classes; the utility returns SerialVersionUIDin an easy-to-copy format. You can use the serialverJDK utility as shown in the example:
$ serialver
use: serialver [-classpath classpath] [-show] [classname...]
$ serialver -classpath . Hello
Class Hello is not Serializable.
$ serialver -classpath . Hello
Hello: static final long SerialVersionUID = -4862926644813433707L;
You can also use the utility serialveras GUIa command $ serialver –show, this will open an inspector serial versionwhich takes the full class name and displays it Serial version.

Summary

Now we know what it is SerialVersionUIDand why it is important to declare it in a Serializable-class, it's time to review some important facts related to Java SerialVersionUID.
  1. SerialVersionUIDused to indicate the version of the serialized data.

  2. When we don't declare SerialVersionUIDin our class, the Java runtime does it for us, but this process is sensitive to many metadata of the class including the number of fields, type of fields, field access modifiers, interfaces that are implemented in the class, etc. You can find the exact information in Serialization documentation from Oracle.

  3. It is recommended to declare SerialVersionUID as a private static final long variable to avoid the default mechanism. Some IDEs, such as Eclipse , also issue warnings if you forget this, for example: "The Serializable class Customer does not declare a static final SerialVersionUID field of type long." . Although you can disable this warning by going to Window > Preferences > Java > Compiler > Errors/Warnings > Potential Programming Problems, I suggest not doing so. Only when data recovery is not required can I be careless about it. Here's what this error looks like in the Eclipse IDE, all you need to do is make the first quick decision. Why use SerialVersionUID inside Serializable class in Java - 2

  4. You can also use the serialver utility from the JDK to generate Serial Version for classes in Java. The utility also has a GUI, which is enabled when passing the - parameter show.

  5. The best practice in serialization is to explicitly declare SerialVersionUID, to avoid any problems with de-serialization, especially if you are working with a client-server application that relies on serialized data, such as RMI.
It's all about SerialVersionUIDin Java. Now we know why it is important to declare correctly SerialVersionUIDin class. You can thank your IDE for this reminder, which could potentially break de-serialization of your class. If you want to read more about serialization and related concepts, you can also check out these awesome articles. Original here
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION