Introduction to serialVersionUID
When it comes to the transmission of objects during reprogramming, we will adopt serialization technology, through serialization, the process of converting the state information of the object into a form that can be stored or transmitted. In Java, we can serialize objects by implementing the Serializable interface, and serialVersionUID is an indispensable role in the Java serialization mechanism. We can verify version consistency by judging the serialVersionUID of the class at runtime. When deserializing, the JVM will compare the serialVersionUID in the passed byte stream with the serialVersionUID of the local corresponding entity (class). If the same is considered to be consistent, it can be deserialized, otherwise an exception with inconsistent serialization versions will occur.
When we use IntelliJ IDEA to write classes and implement Serializable (serialized) interface, we may encounter such a problem, that is:
The serialVersionUID cannot be generated automatically .
serialVersionUID is another very important field, because Java's serialization mechanism verifies serialVersionUID of the class at runtime to verify version consistency. When deserializing, the JVM will compare serialVersionUID in the passed byte stream with serialVersionUID of the local corresponding entity (class). If the same is considered to be consistent, it can be deserialized, otherwise an exception with inconsistent serialization versions will occur.
Generally speaking, there are two ways to define serialVersionUID , namely:
1L , specifically private static final long serialVersionUID = 1L;64 -bit hash field based on the class name, interface name, member methods and attributes, for example, private static final long serialVersionUID = XXXL;There are two main purposes for serialization of Java classes, namely:
Here, let’s take a look at how to use IntelliJ IDEA to automatically generate serialVersionUID .
Step 1: Install the GenerateSerialVersionUID Plugin
As shown in the above figure, click Preferences to enter the following interface:
Here, select Plugins and search for GenerateSerialVersionUID . If you don't find this plugin, you can click Search in repositories to search:
As shown in the above picture, click install to install to install this plug-in.
Step 2: Set the Inspections function
As shown in the above figure, enter Default Settings , in the Inspections settings page, check Serializable class without 'serialVersionUID' , and you can also set prompt levels in Severity , such as Warning , Error , etc., which defaults to Warning . It is also recommended to select Warning level prompts.
As shown in the above figure, create a class and implement the Serializable interface, then press alt + Enter to receive a prompt, and select SerialVersionUID :
As shown in the figure above, we have obviously used IntelliJ IDEA to automatically generate serialVersionUID !
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.