开发者

Serialisation and sub-classes

If I define a class as Serialiable then wh开发者_C百科en I create a sub-class it asks me to define a serialVersionUID. Is this necessary or can I rely on the one in the super-class?


I used to declare once SerialVersionUID in separate (possibly top class), like:

public static final long GlobalSerialVersionUID=0L;

And then in each Serializable class declare:

private static final long serialVersionUID = MyTopClass.GlobalSerialVersionUID;

this approach would guarantee that you will all time have the same SerialVersionUID and if you will be going to implement data interchange between different serialized versions - you could branch code between different serialized versions.


SerialVersionUID is used if you change the implementation of a class, but still want to be able to serialize/deserialise objects saved in the "old" format. So this means that you have to implement it in the subclass as well, if you want to change the subclass but not the superclass. However, if you dont need this "version control" you dont have to use SerialVersionUID at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜