Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About generated serialVersionUID in Eclipse

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?

like image 321
Tapas Bose Avatar asked Apr 30 '12 19:04

Tapas Bose


People also ask

What is the purpose of serialVersionUID?

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.

How do you generate a serialVersionUID for a class?

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.

What is meant by serialVersionUID in Java?

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.

Where can I find serialVersionUID of a class?

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.


4 Answers

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.

like image 68
Jack Avatar answered Oct 13 '22 00:10

Jack


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.

like image 31
ControlAltDel Avatar answered Oct 12 '22 23:10

ControlAltDel


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.

like image 44
user207421 Avatar answered Oct 12 '22 23:10

user207421


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

like image 22
ratchet freak Avatar answered Oct 13 '22 01:10

ratchet freak