Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How serialversionuid is calculated

When I create Java Class in Eclipse which implements Serializable interface, I get a warning

The serializable class ABCD does not declare a static final serialVersionUID field of type long

So when I click on warning, I get a option in Eclipse to

Add generated Serial Version ID

Once I choose that option, Eclipse automatically creates a serialVersionUID variable for me.

Now I wanted to know that on what basis that number is generated. It is a just any random number ? Can I provide any random number of my own ?

like image 594
tiger Avatar asked Oct 12 '11 09:10

tiger


People also ask

How do I find the 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.

How is serialVersionUID implemented?

File -> Settings -> Editor -> Inspections -> Java -> Serialization issues : Find serialization class without serialVersionUID and check it. Back to the editor, clicks on the class name, ALT + ENTER (Windows), it will prompts the Add serialVersionUID field option. A new serialVersionUID is auto-generated.

What is meant by serialVersionUID?

SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. Specifying one gives more control, though JVM does generate one if you don't specify.

How does serialVersionUID work 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.


1 Answers

It is calculated based on the structure of your class - fields, methods, etc. It is specified in the Object Serialization Specification - see this section for the exact format.

The spec describes what happens in no value is provided, but the autogeneration uses the same algorithm.

The sequence of items in the stream is as follows:

  • The class name.
  • The class modifiers written as a 32-bit integer.
  • The name of each interface sorted by name.
  • For each field of the class sorted by field name (except private static and private transient fields:
    • The name of the field.
    • The modifiers of the field written as a 32-bit integer.
    • The descriptor of the field.
  • If a class initializer exists, write out the following:
    • The name of the method, .
    • The modifier of the method, java.lang.reflect.Modifier.STATIC, written as a 32-bit integer.
    • The descriptor of the method, ()V.
  • For each non-private constructor sorted by method name and signature:
    • The name of the method, .
    • The modifiers of the method written as a 32-bit integer.
    • The descriptor of the method.
  • For each non-private method sorted by method name and signature:
    • The name of the method.
    • The modifiers of the method written as a 32-bit integer.
    • The descriptor of the method.
like image 136
Bozho Avatar answered Oct 21 '22 16:10

Bozho