问题描述
Serializable
界面的启动使我感到困惑,为什么我必须在所有类中都包含此字段.我知道此接口需要一个唯一的标识符来标记该类,但是为什么它们不能在运行时生成该接口.例如,他们可以使用完全合格的类名的MD5哈希值或类似的方法(用于在极少数情况下处理重复项)来生成它(我敢肯定,当要求生成ID时,eclipse会执行什么操作).
It has baffled me from the launch of the Serializable
interface why I have to incorporate this field in all of my classes. I understand that this interface needs a unique identifier to mark the class but why cant they generate this at run-time. For instance they could generate it using an MD5 hash of the fully-qualified class name or a similar methodology used to handle duplicates in their rare occurrence (Which is, I'm sure, what eclipse does when asked to generate the id anyway).
所以我要问的是(框架的帖子不只是针对标准库的麻烦),正是框架如何使用序列化字段?
So what I'm asking (no this post isn't just a rant against the standard library) is exactly how the serialization field is used by the framework?
我想知道的原因,因为我将尝试创建一个Aspect(使用AspectJ或其他语言),该Aspect将使用MD5哈希添加serialVersionUID字段,并且能够以一种可以接受的方式处理冲突API.
The reason I would like to know because I am going to try to create an Aspect (in AspectJ or other language) that will add the serialVersionUID field using an MD5 hash and is able to handle collisions in a way that is acceptable to the API.
如果可以运行,我将发布结果.
I will post the results if I can get it working.
推荐答案
不需要具有serialVersionUID
字段.如果您不提供一个,Java将根据您的类的字段和方法生成一个.
There is no requirement to have the serialVersionUID
field. If you don't provide one, Java will generate one based on the fields and methods of your class.
可能要指定serialVersionUID
的原因是为了防止更改方法时更改值,这不会影响序列化的二进制文件.考虑一下课程:
The reason why you might want to specify serialVersionUID
is to prevent the value from changing when methods are changed, which doesn't impact the serialized binary. Consider the class:
public class Person implements Serializable {
private String name;
public String getName() {
return name;
}
}
未指定serialVersionUID
.如果运行serialver Person
,它将返回:
No serialVersionUID
was specified. If you run serialver Person
it returns:
Person: static final long serialVersionUID = 3793453319058452486L;
现在,您决定添加一个方法,但字段保持不变.
Now you decide to add a method but leave the fields the same.
public class Person implements Serializable {
private String name;
public String getName() {
return name;
}
public Object foo() {
return "bar";
}
}
序列化的二进制文件仍然与旧版本完全兼容,但是serialVersionUID
是不同的:
The serialized binary is still fully compatible with the old version, but the serialVersionUID
is different:
Person: static final long serialVersionUID = -6188734029437600310L;
使用不同的serialVersionUID
,反序列化将导致serialVersionUID不匹配错误.解决方法是将自己的serialVersionUID
设置为任何值(我将其设置为1L
),并在字段更改时进行更改.
With a different serialVersionUID
, deserializing will result in a serialVersionUID mismatch error. The workaround is declare your own serialVersionUID
by setting it to any value (I set it 1L
), and changing it whenever the fields have changed.
另请参见与此相关问题什么是serialVersionUID,为什么要使用它?" 进行更详细的讨论.
Also see this related question "What is a serialVersionUID and why should I use it?" for a more detailed discussion.
这篇关于为什么serialVersionUID字段存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!