[英]Does each Java class in an app need to have a different unique serial version ID?
From I read online, it seems a good practice to create a serial version ID for a class. 从我的在线阅读中可以看出,为类创建序列号ID似乎是一种好习惯。
My question is that in an application, do I have to create an unique ID for each class? 我的问题是,在应用程序中,是否必须为每个类创建唯一的ID?
Or can I simply assign the following ID to each class in my application? 还是可以简单地将以下ID分配给应用程序中的每个类?
private static final long serialVersionUID = 1;
Serializable
Serializable
No, it doesn't matter what the ID is for the specific class - it's the serialization format version for that class . 不,这并不重要的ID是特定阶级-这是序列化格式版本该类 。 The class name itself is still serialized, so it's not like it has to be unique across all classes.
类名本身仍是序列化的,因此它不必在所有类中都唯一。 (Of course, you may find other ways in which having a different version number for each class would be useful. I can't think of any off hand, but that's a different matter.)
(当然,您可能会发现其他方法,每个类使用不同的版本号将很有用。我想不出任何办法,但这是另一回事。)
Using a constant value is fine - but you do need to make sure you change it when you've genuinely changed the serialization format (eg by adding a field). 用一个恒定值是好的-但你必须确保你改变它,当你真正改变的序列化格式(例如,通过添加一个字段)。
Mind you, I'd prefer to use something like Protocol Buffers which is rather more graceful with backward/forward compatibility :) 提醒您,我更喜欢使用协议缓冲区之类的东西,因为它具有向后/向前兼容性:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.