The serializable class XXX does not declare a static final serialversionuid field of type long

Source: Internet
Author: User

Today, when I was writing a Java program, I found this warning prompt, so I Googled it. The answer is as follows:

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 stronugly recommended that all serializable classes explicitly declare serialversionuid values, since the default condition 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 implements SS different Java compiler implementations, A serializable class must declare an explicit serialversionuid value. it is also stronugly advised that explicit extends declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class-serialversionuid fields are not useful as inherited members.

The above is the description of serializable in javadoc.

It also says:

Serialversionuid is used as the version identifier in Java object serialization;
If a serialization class does not declare the output of such a static final, JVM will calculate one for this class based on various parameters;
For the same class, JDK of different versions may produce different serivalversionuid;

The serializable class XXX does not declare a static final serialversionuid field of type long
Serialversionuid:
To maintain version compatibility during serialization, that is, during version upgrade, deserialization still maintains the uniqueness of objects.

You can write one at a time and generate one for you in eclipse. There are two ways to generate one:
One is the default 1l, for example, 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 attribute, for example, Private Static final long serialversionuid =-8940196742313994740l.

When a class implements the serializable interface and does not define the serialversionuid, eclipse will provide this prompt function for you to define.
In eclipse, click the "warning" icon in the class, and eclipse will automatically generate two methods, as described above. If you do not want to define it, you can disable it in eclipse settings as follows:
Window ==> preferences ==> Java ==> compiler ==> error/warnings ==> potential Programming Problems
Change the value of serializable class without serialversionuid to ignore.

If you do not consider compatibility issues, turn it off, but this function is good. If serializable is implemented in any category, if serialversionuid is not added, eclipse will give you a warning that this serialversionuid is backward compatible for this type of serializable.

If your class serialized is stored on the hard disk, but then you change the field of the category (increase, decrease, or rename), when you deserialize, an exception will occur, this will cause incompatibility issues.

However, when the serialversionuid is the same, it will deserialize different fields with the default value of type, which can avoid incompatibility issues.

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.