[Java version] The story of serialization

What is serialVersionUID

Runtime serialization assigns a version number to every serialization class. It's serialVersionUID.

It performs authentication, such as consistency checks, when deserializing a serialized class. If the serialVersionUID of the deserialized class and the class in the class loader are different, The deserialization process raises a ʻInvalidClassException` exception.

Serializable classes can define their own serialVersionUID. However, the serialVersionUID field must be static, long, and final. Example:

private static final long serialVersionUID = 42L;

If the serializable class does not explicitly define the serialVersionUID field, then from runtime serialization The value of serialVersionUID is automatically generated by calculating based on the structure of the class such as the fields and methods of the class.

Nevertheless, it is highly recommended that all serializable classes explicitly declare serialVersionUID. The reason is that the calculation method of serialVersionUID depends on the structure of the class (it seems to take a long time to calculate) and the Java compiler. Unexpected ʻInvalidClassExceptions` exceptions may occur when doing deserialization time.

That's why the class explicitly sets serialVersionUID to guarantee the same serialVersionUID in different Java compiler environments. Should be declared.   We also strongly recommend that you use the private modifier in the most explicit serialVersionUID declaration.

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: ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L; 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. 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 deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible.

Recommended Posts

[Java version] The story of serialization
The story of writing Java in Emacs
The story of low-level string comparison in Java
The story of making ordinary Othello in Java
[Java] Access the signed URL of s3 (signed version 2)
The story of learning Java in the first programming
Try the free version of Progate [Java II]
Try the free version of Progate [Java I]
Switch the version of bundler
[Java] Check the JDK version of the built war file
The story of making dto, dao-like with java, sqlite
[Java] Delete the elements of List
Java serialization
Check the version of Cent OS
20190803_Java & k8s on Azure The story of going to the festival
Story of passing Java Gold SE8
The story of @ViewScoped consuming memory
The story of pushing Java to Heroku using the BitBucket pipeline
The origin of Java lambda expressions
The story of building a Java version of Minecraft server with GCP (and also set a whitelist)
Switch the version of java installed by SDKMAN when moving directories
The story of not knowing the behavior of String by passing Java by reference
A story about hitting the League Of Legends API with JAVA
Is the version of Elasticsearch you are using compatible with Java 11?
The story of encountering Spring custom annotation
Get the result of POST in Java
Story of paiza.jp [Java standard input solution]
About the version of Docker's Node.js image
Check the contents of the Java certificate store
Examine the memory usage of Java elements
[Java] Get the day of the specific day of the week
Memo: [Java] Check the contents of the directory
The story of updating SonarQube's Docker Container
Installation of OMC APM Agent --Java version-
Compare the elements of an array (Java)
The story of RxJava suffering from NoSuchElementException
The story of AppClip support in Anyca
[day: 5] I summarized the basics of Java
What are the updated features of java 13
Easily measure the size of Java Objects
Looking back on the basics of Java
How to install the legacy version [Java]
Output of the book "Introduction to Java"
The story received by Java SE11 silver
Check the version of the standard Web software.
[Java] Check the number of occurrences of characters
[Java] [Spring] Test the behavior of the logger
Java static story
The story of forgetting to close a file in Java and failing
The story of making a game launcher with automatic loading function [Java]
Java version check
The story of acquiring Java Silver in two months from completely inexperienced.
How to find out the Java version of a compiled class file
Java serialization notes
[Java] Overview of Java
Java initializer story
Java generic story
Regarding the transient modifier and serialization in Java
Check the version of the JDK installed and the version of the JDK enabled
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file