Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out the serialVersionUID of a serialized Java object?

I have several serializable classes that were compiled without specifying a serialVersionUID. I now need to add some data members to these classes but wish to preserve some already serialized objects.

Is there anyway to find out the serialVersionUID of these serialized objects so that I can specify the same ID in the source code?

like image 818
Matthew Blackford Avatar asked Apr 01 '09 00:04

Matthew Blackford


People also ask

How do I find serialVersionUID?

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.

Is serialVersionUID serialized?

The serialization at runtime associates with each serializable class a version number called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.

What is default serialVersionUID 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.

What is the role of serialVersionUID in serialization process?

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.


1 Answers

If you haven't modified the code yet you could probably run the serialver tool (part of the JDK) and see what it generates. I believe that will get you what the calculated seriVersionUID is.

Here is a brief article on serialver.

like image 144
TofuBeer Avatar answered Oct 04 '22 15:10

TofuBeer