11

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?

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • 1
    Why would you want *different* classes to have serial serialVersionUIDs? – Andy Thomas Apr 30 '12 at 19:13
  • It is my programming style to generated serialVersionUID serially. – Tapas Bose Apr 30 '12 at 19:18
  • 2
    That's one common style across the versions of a single class. Does your style across multiple classes have a benefit? When you later want to version a particular class, to what do you set that class's serialVersionUID, and what happens to the serial relationship among classes? – Andy Thomas Apr 30 '12 at 19:56

4 Answers4

17

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.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 2
    What about `@SuppressWarnings("serial")`? How would this change things? Recommended, not recommended? Avoid? – ADTC Sep 20 '13 at 04:21
  • 2
    @ADTC it works, but is subject to compiler incompatibilities. See Serializable interface. From Java 7: “If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, […] may vary depending on compiler implementations, and can thus result in unexpected” – Kissaki Sep 12 '14 at 08:41
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.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Thanks for the answer. Yes I know why sserialVersionUID has been used, but it is my programming style to generate them serially in a project in Eclipse. – Tapas Bose Apr 30 '12 at 19:20
2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • If you want your class to be useful for a long time, you should only make "compatible changes", all of which are defined in the Object Serialization Specification that @EJP mentions, here. – Christopher Schultz Dec 03 '15 at 21:50
1

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")

ratchet freak
  • 47,288
  • 5
  • 68
  • 106