The function of Serialversionuid and its setting method

Source: Internet
Author: User
Tags object serialization

Disclaimer: This article is reproduced

http://blog.csdn.net/kakaxi_77/article/details/8129070

http://snowlotus.iteye.com/blog/247129

The role of Serialversionuid in Java files

http://blog.csdn.net/hulefei29/article/details/2823221

The role of Serialversionuid

Http://www.blogjava.net/invisibletank/archive/2007/11/15/160684.html

Explanation of Serialversionuid in Java

Http://www.blogjava.net/sealyu/archive/2009/04/21/266759.html

The function of Serialversionuid and its setting method

0. Preface

You will often see code such as this:

[Java]View PlainCopy
    1. Private static final long serialversionuid = 876323262645176354L;


Often confused, the following summarizes to explain:

An overview

Why do I need to overload the Serialversionuid property in some Java classes?
In Java, software compatibility is a big problem, especially when it comes to object serialization, where an object is already serialized,
But this object has been modified and redeployed, in this case, it is not difficult to use the old software to read the new file format, but it is possible to lose some information.

Serialversionuid to solve these problems, the new serialversionuid must be defined in the following form: Static final long serialversionuid=-2805284943658356093l;.
Where the number followed by the L indicates that this is a long value. This way to solve the problem of crosstalk between different versions.

--------------------------------------------------------------------------------------------------------------- -------------------

Serialversionuid used to indicate compatibility between different versions of a class

In simple terms, the serialization mechanism of Java verifies version consistency by judging the serialversionuid of the class at run time. When deserializing, the JVM compares the serialversionuid in the stream that is transmitted to the serialversionuid of the local corresponding entity (Class), and if the same is considered consistent, it can be deserialized, otherwise the serialized version inconsistency exception will occur.

When an entity (class) that implements the Java.io.Serializable interface does not explicitly define a variable named Serialversionuid, which is of type long, The Java serialization mechanism automatically generates a serialversionuid as a serialized version based on the compiled class, in which case only the class generated by the same build will generate the same serialversionuid.

If we do not want to compile to force the software version, that is, the entity that implements the serialization interface is compatible with the previous version, the class that does not change, it is necessary to explicitly define a variable named Serialversionuid, type Long, Serialized entities that do not modify the value of this variable can serialize and crossdress each other.

--------------------------------------------------------------------------------------------------------------- --------------

Two explanations

Explanation of Serialversionuid in Java

Serialversionuid Effect:
Serialization preserves the uniqueness of the object in order to maintain version compatibility, that is, deserialization while the version is being upgraded.
There are two ways to build:
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 property, for example:
Private static final long serialversionuid = XXXXL;


When you implement a class with a serializable interface, if there is no definition serialversionuid,eclipse will provide this
The hint function tells you to define. In Eclipse, click the warning icon in the class and Eclipse will
Automatically given two ways of generating. If you do not want to define it, the settings in eclipse also
You can turn it off, set the following:
Window ==> Preferences ==> Java ==> Compiler ==> error/warnings ==>
Potential programming problems
Change the warning of the serializable class without serialversionuid to ignore.


If you do not consider the compatibility issue, you turn it off, but this function is good, as long as any category implemented serializable this interface, if not joined Serialversionuid,eclipse will give you warning hint, This serialversionuid to make the category serializable backwards compatible.

If your class serialized on the hard drive, but then you change the category field (increase or decrease or rename), when you deserialize, it will appear exception, which will cause incompatibilities.

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

--------------------------------------------------------------------------------------------------------------- ------------

Three settings

The function of Serialversionuid and its setting method

Set Serialversionuid default build mode: private static final long serialversionuid = 1L;
Serialversionuid role: Serialversionuid is used to indicate compatibility between different versions of a 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.
In the JDK, you can use the Serialver.exe tool under the Bin directory of the JDK to generate this serialversionuid, and for Test.class, execute the command: Serialver Test.
In order to ensure the compatibility of class versions when deserializing, 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. 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, the property value is computed 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 implement this property value, the calculation strategy may be different, resulting in the fact that the class does not change, but because the JVM is different, there is a phenomenon that cannot be correctly deserialized due to incompatible class versions.

Serialversionuid function and setting method (RPM)

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.