"Private static final long Serialversionuid = 1L" appears when creating a servlet class What does the statement mean?

Source: Internet
Author: User

Implementing java.io.Serializable This interface is for serialization, and Serialversionuid is used to indicate compatibility between different versions of the serialization class. If you modify this class, you want to modify this value.

Otherwise, an error occurs when a class that was previously serialized with the old version of the class is restored.

After implementation if you use the tool will come out warning, he will prompt you, you can automatically generate private static final long serialversionuid = 1L; In order to deserialize, the

To ensure compatibility of class versions, it is best to include the private static final long Serialversionuid attribute in each class to be serialized, with a specific value defined by itself.

In the JDK, you can use the Serialver.exe tool in the bin directory of the JDK to generate this serialversionuid, such as for Test.class executable command: Serialver Test.

This way, even if a class has been modified after the corresponding object has been serialized, the object can still be deserialized correctly. Otherwise, if the property is not explicitly defined,

This property value is calculated by the JVM based on the information about the class, and the modified class evaluates to a different result than the pre-modified class, causing the object's deserialization to fail because the class version is incompatible.

Another disadvantage of not explicitly defining this property value is that it is not conducive to the porting of programs between different JVMs. Because different compilers can implement this property value in a different calculation strategy,

This causes the class to not change, but because the JVM is different, there is a phenomenon that cannot be properly deserialized due to incompatible class versions. When our system does not need to serialize classes very often,

You can remove these warnings and make the following settings: Window-->preferences-->java, change the settings for serializable class without serialversionuid from warning to ignore.

Then eclipse compiles the program, and the warning messages disappear.

The final suggestion is that if we develop a large number of classes that need to be serialized, we'd better revert to the original settings. This will ensure the performance and robustness of the system.

About Serialversionuid's explanation

Serialversionuid Effect: Serialization preserves the uniqueness of the object in order to maintain the compatibility of the version, that is, deserialization while the version is being upgraded.

You can write a random, in eclipse it generates one for you, there are two ways to build: One is the default 1L, such as: private static final long serialversionuid = 1L;

One is to generate a 64-bit hash field based on the class name, interface name, member method, and property.

For example: private static final long serialversionuid = -8940196742313994740l;

Eclipse prompts the Serializable class XXX does not declare a static final serialversionuid field of type Long that is eclipse provides this hint function to You.

You order it warning icon Two and eclipse will be given automatically.

If you don't like it, you can turn it off, potential programming problems, error/warnings-preferences, compiler, Windows---Seriali Zable class without serialversionuid warning changed to ignore.

In fact, if you don't consider the compatibility issue, then turn it off. In fact, it is good to have this function. As long as any category is actually serializable this interface,

If not joined Serialversionuid,eclipse will give you warning hint, this serialversionuid in order to make the category serializable compatible.

Consider if your class serialized is saved to the hard drive today, but then you change the category field (increase or decrease or rename). When you deserialize, there will be exception.

This will make the problem of incompatibility.

But when Serialversionuid is the same, it deserialize the different field with the default value of type. This avoids the problem of incompatibility.

Exception Status: The Serializable class XXX does not declare a static final serialversionuid field of type long

"Private static final long Serialversionuid = 1L" appears when creating a servlet class What does the statement mean?

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.