简体   繁体   English

应用程序中的每个Java类都需要具有不同的唯一串行版本ID吗?

[英]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;

  • You only need a serial version ID for classes that will be serialized, ie mostly those that implement Serializable 对于将要序列化的类,您只需要一个序列号ID,即大多数实现了Serializable
  • The ID is per class, it's not a problem if two classes have the same ID是每个类的,如果两个类具有相同的对象,这不是问题
  • It's only necessary if there is a chance that serialized instances will be deserialized by a different version of the program. 只有在序列化的实例有可能被程序的不同版本反序列化时,才有必要。 This is often not the case. 通常情况并非如此。

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.

相关问题 NetBeans是否可以为Java类生成自动串行版本ID? - Can NetBeans generate an automatic serial version ID for a Java class? 类在 Java 中是否具有(唯一)标识? - Does a class have a (unique) identity in Java? POJO类中序列号id的唯一性 - Uniqueness of serial version id in POJO Class 出于警报管理器的目的,未决意图对于应用程序或设备是否具有唯一的ID? - For purposes of Alarm Manager, does a pendingintent have unique id for app or for device? 在Java Serlization中,我们必须使用哪种串行版本ID生成方法? - In Java Serlization which Serial version ID generation approach we have to use? Kotlin 的 Java 版本是否具有相同的限制? - Does Kotlin's version of Java's for each have the same limitations? 在一个Java应用程序中需要两个不同版本的JAR - Need two different version JARs in one Java app Java,具有彼此不同方法的Class对象? - Java, Class objects that have different methods to each other? 如果两个Java框架需要第三个框架,但两个框架都需要不同版本的第三个框架,会发生什么情况? - What happens when two Java frameworks need third one but each of the two need different version of third one? 用于提供唯一 ID 的 Java 抽象类不起作用 - 所有对象都具有相同的 ID - Java abstract class for giving unique ID not working - all of objects have same ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM