Private Static final long serialversionuid Function

Source: Internet
Author: User

Private Static final long serialversionuid = ???

What is the purpose of this variable?

Generally, when is this variable added? What are the requirements for its value?

You can understand it as follows:
Serialversionuid is used to indicate the compatibility between different versions of the class. If you modify this class, you need to modify this value. Otherwise, an error occurs when the class serialized with the old version is restored. To ensure the compatibility of the class version during deserialization, it is best to add the Private Static final long serialversionuid attribute to each class to be serialized. The specific values are defined by yourself.
Click the left button on the warning to automatically add it. You can also remove the warning. Settings: window --> preferences --> JAVA, set the serializable class without serialversionuid from warning to ignore. Then eclipse will re-compile the program, and the warning information will disappear.

Google. 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 strongly recommended that all serializable classes explicitly declare incluvalues, since the default constraint computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus
Result In unexpected specify during deserialization. Therefore, to guarantee a consistent serialversionuid value pair SS different Java compiler implementations, A serializable class must declare an explicit implements value. It is also
Stronugly advised that explicit serialversionuid 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.

Finally, without considering compatibility, I changed this warning to ignore.

Private Static final long serialversionuid = ???

What is the purpose of this variable?

Generally, when is this variable added? What are the requirements for its value?

You can understand it as follows:
Serialversionuid is used to indicate the compatibility between different versions of the class. If you modify this class, you need to modify this value. Otherwise, an error occurs when the class serialized with the old version is restored. To ensure the compatibility of the class version during deserialization, it is best to add the Private Static final long serialversionuid attribute to each class to be serialized. The specific values are defined by yourself.
Click the left button on the warning to automatically add it. You can also remove the warning. Settings: window --> preferences --> JAVA, set the serializable class without serialversionuid from warning to ignore. Then eclipse will re-compile the program, and the warning information will disappear.

Google. 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 strongly recommended that all serializable classes explicitly declare incluvalues, since the default constraint computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus
Result In unexpected specify during deserialization. Therefore, to guarantee a consistent serialversionuid value pair SS different Java compiler implementations, A serializable class must declare an explicit implements value. It is also
Stronugly advised that explicit serialversionuid 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.

Finally, without considering compatibility, I changed this warning to ignore.

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.