当向我的 Java 程序添加文档时,我意识到大多数类都需要声明 serialVersionUID
常量属性。我到底应该如何记录该属性(property)?如果我使用默认的和生成的串行版本 UID,我的记录是否会有所不同?
/**
* What goes here? "A unique serial version identifier"
*/
private static final long serialVersionUID = -8922096951749901688L;
最佳答案
首先,您提供的serialVersionUID
注释似乎是正确的但不必要。
serialVersionUID
是 Serializable
对象(并非所有 Java 类)的一部分,并在该对象的序列化/反序列化期间使用对象。
作为一般规则。 始终检查 API
在本例中,可序列化
API,位于底部!!
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. 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. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
So you're saying that it should actually not be documented whatsoever because there is javadoc for it already? When I omit the javadoc and mouseover it, it does not show anything
:/
它是 Serialized
接口(interface)的一部分,因此恕我直言,没有必要......无论如何,您可以使用自己的注释 + @see
注释。像这样的东西(不确定是否有效,现在无法创建 javadoc...)
/**
* A unique serial version identifier
* @see Serializable#serialVersionUID
*/
关于java - SerialVersionUID JavaDoc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40407468/