Describes how to automatically generate a serialVersionUID in IntelliJ IDEA ),

Source: Internet
Author: User

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:

  1. Use the default1L, Specificallyprivate static final long serialVersionUID = 1L;
  2. Generate64For exampleprivate static final long serialVersionUID = XXXL;

Java classes are serialized for the following purposes:

  1. Stores the object's byte sequence permanently on the hard disk, usually in a file;
  2. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.