Java.io.InvalidClassException exception resolution, the implementation of serializable interface considerations

Source: Internet
Author: User

Solution: Explicitly specify in the class

Private static final long serialversionuid = 42L;

class implements the serialization interface, and when serialization is deserialized, throws a Java.io.InvalidClassException exception


Java.io.InvalidClassException:com.xx.Xxx; Local class Incompatible:stream Classdesc serialversionuid = -783991920331, local class Serialversionuid = 331138183213

This exception is caused by the serialversionuid of the current class and the Serialversionuid of the class bytes in the class after deserialization, the serialversionuid if it is not explicitly declared in the class, it is through the class name, A number of factors, such as method names, are calculated, theoretically the one by one mapping relationship, which is the only

Declaration of the Serializable interface in the JDK

The serialization runtime associates with each serializable Class A version number,called a serialversionuid, which is used during deserialization to verify that the sender and receiver of a serialized Object has loaded classes for this object that is compatible with respect to serialization.If The receiver has loaded a class for the object, the have a different serialversionuid than that of the corresponding Sender ' s class, then deserialization'll result in an invalidclassexception. A serializable class can declare its own serialversionuid explicitly by declaring a field named "Serialversionuid" that Mu   St be static, final, and of type Long:any-access-modifier static final long serialversionuid = 42L; If a serializable class does not explicitly declare a serialversionuid and then the serialization runtime would calculate a de Fault Serialversionuid value for this class based on various aspects of the class, as described in the Java (TM) Object Ser Ialization specification. However,It is strongly recommended this all serializable classes explicitly declare Serialversionuid values, since the default Serialversionuid computation is highly sensitive to class details which may vary depending on compiler implementations, and can thus result in unexpected invalidclassexceptions during deserialization.  Therefore, to guarantee a consistent Serialversionuid value across different Java compiler implementations, a serializable Class must declare an explicit SERIALVERSIONUID value. It is also strongly advised this explicit SERIALVERSIONUID declarations use the private modifier where possible, since suc  H declarations apply only to the immediately declaring Class--serialversionuid fields is not useful as inherited members. Array classes cannot declare an explicit serialversionuid, so they always has the default computed value, but the Requir Ement for matching serialversionuid values are waived for array classes

Important points:

1. Explicit declaration of serialization ID is recommended for all classes that implement serialization

2. The access type of the serialization ID is recommended as private because it is used only within itself and does not flow to subclasses because of inheritance

3. The array is unable to display the Declaration serialization ID (for example, string[], where you cannot declare serialversionuid), but the Java serialization does not serialversionuid compare the array object

Java.io.InvalidClassException exception resolution, the implementation of serializable interface considerations

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.