Describes how to automatically generate a serialVersionUID in IntelliJ IDEA ),
Introduction to serialVersionUID
During the re-programming process, when the object transmission problem is involved, we will adopt the serialization technology. Through serialization, the object state information can be converted into a stored or transmitted process. 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. During deserialization, the JVM compares the serialVersionUID In the byte stream with the serialVersionUID of the corresponding local entity (class). If they are the same, they can be deserialized, otherwise, the serialization version is inconsistent.
When we use IntelliJ IDEA to write classes and implement the Serializable (serialization) interface, we may encounter the following problem:
Unable to automatically generate serialVersionUID.
WhileserialVersionUIDIt is also a very important field, because the Java serialization Mechanism judges the class at runtime.serialVersionUIDTo verify version consistency. During deserialization, JVM willserialVersionUIDCorresponding to the local object (class)serialVersionUIDFor comparison, if they are the same, they are considered to be consistent and can be deserialized. Otherwise, the serialization version is inconsistent.
GenerallyserialVersionUIDThere are two methods:
- Use the default
1L, Specificallyprivate static final long serialVersionUID = 1L;
- Generate
64For exampleprivate static final long serialVersionUID = XXXL;
Java classes are serialized for the following purposes:
- Stores the object's byte sequence permanently on the hard disk, usually in a file;
- Transmits the object's byte sequence over the network.
Here, let's take a look at how to use IntelliJ IDEA to automatically generateserialVersionUID.
Step 2: Install the GenerateSerialVersionUID plug-in
As shown in, clickPreferencesTo go to the following page:
Here, selectPlugins, And searchGenerateSerialVersionUIDIf this plug-in is not found, clickSearch in repositoriesSearch:
As shown in, clickinstallTo install the plug-in.
Step 2: Set Inspections
As shown in, goDefault Settings, InInspectionsOn the settings page, selectSerializable class without 'serialVersionUID'InSeveritySet the prompt level, as shown in figureWarning,Error. The default value isWarning, We recommend that you selectWarningLevel prompt.
As shown in, create a class and implementSerializableInterface, and then pressalt+EnterKey to receive the prompt, and then selectSerialVersionUID:
As shown in, it is clear that we have used IntelliJ IDEA to automatically generateserialVersionUIDNow!
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.