问题
Is there any way to generate serialVersionUID in Eclipse serially? By serially I want to mean that if one serializable class has serialVersionUID = 1L, then when I generate serialVersionUID of another class this will be serialVersionUID = 2L.
If I manually specify 1L, 2L, 3L and so on, will this can create any problem?
Eclipse gave an option to choose "Add generated serial version ID", is this option safe to choose?
回答1:
No, it won't create any problem in any of your two circumstances:
- you can manually specify it incrementally
- you can let Eclipse assign them random generated values
- you can keep them all
1L
for different classes
The purpose of the serial UID is to forbid serialization and deserialization of same classes in different versions, especially when there is not forward compatibility (eg. new version of the class cannot be serialized/unserialized by a previous definition).
This is a circumstance that occurs really rarely, especially when working with your own project which are not part of big frameworks that rely on serialization. So you can safely do whatever you want.
The situation in which you want to have different serials for the same class is when you are updating a class and you want to forbid the serialization of it with a previous declaration of the SAME class.
回答2:
Let me answer your questions backwards:
Add generated serial version ID... is it safe? Answer: Yes
If you manually specify 1L, 2L, 3L is that a problem: No more of a problem than making them all 1 or any other number
generating serial ids serially: The answer is there is no built-in mechanism for doing this. The fact that you are asking this question leads me to believe you don't know what the serialVersionUID is for. Even if you have already read up on what this does, you should read it again.
回答3:
You shouldn't do this at all. The idea is that you keep the serialVersionUID
constant for as long as possible during the evolution of a class, by observing the rules in the Object Versioning section of the Object Serialization Specification.
Your 'programming style' has nothing to do with it. It is a matter of using the thing for the purpose it was designed for. It is not designed for the purpose you are using it for. Source code control systems can do that for you.
回答4:
the java serialization supports that all classes have the same serialVersionUID
this is use for version control so that serialized objects from a older definition are incompatible with a newer version
check http://java.sun.com/developer/technicalArticles/Programming/serialization/ for the specifics (scroll down to "version control")
来源:https://stackoverflow.com/questions/10389180/about-generated-serialversionuid-in-eclipse