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?
Simply put, we use the serialVersionUID attribute to remember versions of a Serializable class to verify that a loaded class and the serialized object are compatible. The serialVersionUID attributes of different classes are independent. Therefore, it is not necessary for different classes to have unique values.
You can generate SerialVersionUID in intellij by pressing ctrl + shift +A (find action) and then typing Serializable class without serialVersionUID and toggle it from Off to On.
The SerialVersionUID must be declared as a private static final long variable in Java. This number is calculated by the compiler based on the state of the class and the class attributes. This is the number that will help the JVM to identify the state of an object when it reads the state of the object from a file.
for Android developers, you can also enable serialization inspection from Settings -> Editor -> Inspections -> enable "Serializable class without 'serialVersionUID' check" first, then ALT+ ENTER, the studio will create serialVersionUID for you.
No, it won't create any problem in any of your two circumstances:
1L
for different classesThe 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.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With