Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

java - 什么是serialVersionUID,为什么要使用它?(What is a serialVersionUID and why should I use it?)

Eclipse issues warnings when a serialVersionUID is missing.

(当缺少serialVersionUID时,Eclipse会发出警告。)

The serializable class Foo does not declare a static final serialVersionUID field of type long

(可序列化的类Foo没有声明类型为long的静态最终serialVersionUID字段)

What is serialVersionUID and why is it important?

(什么是serialVersionUID ,为什么重要?)

Please show an example where missing serialVersionUID will cause a problem.

(请显示一个示例,其中缺少serialVersionUID会导致问题。)

  ask by ashokgelal translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The docs for java.io.Serializable are probably about as good an explanation as you'll get:

(您将得到有关java.io.Serializable的文档最佳解释:)

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 have loaded classes for that object that are compatible with respect to serialization.

(序列化运行时与每个可序列化的类关联一个版本号,称为serialVersionUID ,该序列号在反序列化期间用于验证序列化对象的发送者和接收者是否已加载了该对象的与序列化兼容的类。)

If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException .

(如果接收者已为该对象加载了一个与相应发送者类具有不同的serialVersionUID的类,则反序列化将导致InvalidClassException 。)

A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long :

(可序列化的类可以通过声明一个名为serialVersionUID的字段来显式声明其自己的serialVersionUID ,该字段必须是静态的,最终的且类型为long :)

 ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L; 

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.

(如果可序列化的类未显式声明serialVersionUID ,则序列化运行时将根据该类的各个方面,为该类计算默认的serialVersionUID值,如Java(TM)对象序列化规范中所述。)

However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization.

(但是, 强烈建议所有可序列化的类显式声明serialVersionUID值,因为默认的serialVersionUID计算对类详细信息高度敏感,而类详细信息可能会根据编译器的实现而有所不同,因此可能在反序列化期间导致意外的InvalidClassExceptions 。)

Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value.

(因此,为了保证不同Java编译器实现之间的serialVersionUID值一致,可序列化的类必须声明一个显式的serialVersionUID值。)

It is also strongly 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.

(强烈建议显式serialVersionUID声明在可能的情况下使用private修饰符,因为此类声明仅适用于立即声明的类serialVersionUID字段作为继承成员不起作用。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...