Serialization
and
SerialVersionUID
always remains a mystery to many Java developers. I often see questions about what is
SerialVersionUID
, or what happens if I don't declare
SerialVersionUID
in my
Serializable
-class?
Apart from being confusing and infrequently used, another reason for this question is the Eclipse IDE's missing warning
SerialVersionUID
, for example: "
The Serializable class Customer does not declare a static final SerialVersionUID fieldSerializable
of
Customer
type
SerialVersionUID
long " long"). In this article, you will not only learn the basics of Java
SerialVersionUID
but also its impact on the process of serialization and de-serialization. When you declare a class, as
Serializable
by implementing a token interface
java.io.Serializable
, the Java runtime stores an instance of that class on disk using the default serialization mechanism unless you configure the process to use
the Externalizable interface . During serialization, the Java runtime creates a version number for the class so that it can deserialize it later. In Java, this version number is known as
SerialVersionUID
. If, during deserialization,
SerialVersionUID
there is no match, then the process will exit with an exception
InvalidClassException
in the "
main
" stream
java.io.InvalidClassException
, and will also print the class name and the corresponding
SerialVersionUID
. A quick solution to fix this problem is to copy
SerialVersionUID
and define it as a type constant
private
static
final
long
in your class. In this article, we will learn about why we should use
SerialVersionUID
in Java and how to use the serialver JDK tool to generate this ID. If you are new to serialization, you can also watch
Top 10 Java Serialization Interview Questions to assess your knowledge and find gaps in your understanding for further reading. Like
Concurrency
(concurrency) and
Multi-threading
(multithreading),
Serialization
(serialization) is another topic that deserves reading several times.
Why use SerialVersionUID in Java
As I said, when we haven't defined a value
SerialVersionUID
as
static
final
long
in our class, the serialization mechanism will do it for us. This mechanism is sensitive to many details, including the fields of your class, their access modifiers, the interfaces it implements, and even different compiler implementations; any changes to the class or use of a different compiler may produce a different result,
SerialVersionUID
which will ultimately stop the serialized data from being reloaded. It's risky to rely on Java's serialization mechanism to generate this id, which is why it's a good idea to explicitly define it
SerialVersionUID
in your
Serializable class . I highly recommend reading the Java classic -
Joshua Bloch “Effective Java” to understand Java serialization and the problems of handling them incorrectly. By the way, the JDK also provides a tool
serialver
, located in the
bin directory of the JAVA_HOME directory , on my computer,
C:\Program Files\Java\jdk1.6.0_26\bin\serialver.exe , which can be used to generate
SerialVersionUID
for old classes. This is very useful in case you have made changes to your class that breaks serialization and your application is unable to reload serialized instances. You can easily use this utility to create
SerialVersionUID
old instances, and then use it explicitly by declaring the field as
private static final long SerialVersionUID
. By the way, it is highly recommended for performance and security reasons to use the regular binary format for serialization; again, “Effective Java” has several paragraphs that demonstrate the advantages of the regular format in great detail.
How to use serialver JDK utility to generate SerialVersionUID
You can use
serialver
to generate
SerialVersionUID
for classes. This is especially useful for developing classes; the utility returns
SerialVersionUID
in an easy-to-copy format. You can use the
serialver
JDK utility as shown in the example:
$ serialver
use: serialver [-classpath classpath] [-show] [classname...]
$ serialver -classpath . Hello
Class Hello is not Serializable.
$ serialver -classpath . Hello
Hello: static final long SerialVersionUID = -4862926644813433707L;
You can also use the utility
serialver
as
GUI
a command
$ serialver –show
, this will open an inspector
serial version
which takes the full class name and displays it
Serial version
.
Summary
Now we know what it is
SerialVersionUID
and why it is important to declare it in a
Serializable
-class, it's time to review some important facts related to Java SerialVersionUID.
SerialVersionUID
used to indicate the version of the serialized data.
- When we don't declare
SerialVersionUID
in our class, the Java runtime does it for us, but this process is sensitive to many metadata of the class including the number of fields, type of fields, field access modifiers, interfaces that are implemented in the class, etc. You can find the exact information in Serialization documentation from Oracle.
- It is recommended to declare SerialVersionUID as a private static final long variable to avoid the default mechanism. Some IDEs, such as Eclipse , also issue warnings if you forget this, for example: "The Serializable class Customer does not declare a static final SerialVersionUID field of type long." . Although you can disable this warning by going to Window > Preferences > Java > Compiler > Errors/Warnings > Potential Programming Problems, I suggest not doing so. Only when data recovery is not required can I be careless about it. Here's what this error looks like in the Eclipse IDE, all you need to do is make the first quick decision.
- You can also use the serialver utility from the JDK to generate Serial Version for classes in Java. The utility also has a GUI, which is enabled when passing the - parameter
show
.
- The best practice in serialization is to explicitly declare
SerialVersionUID
, to avoid any problems with de-serialization, especially if you are working with a client-server application that relies on serialized data, such as RMI.
It's all about
SerialVersionUID
in Java. Now we know why it is important to declare correctly
SerialVersionUID
in class. You can thank your IDE for this reminder, which could potentially break de-serialization of your class. If you want to read more about serialization and related concepts, you can also check out these awesome articles.
Original
here
GO TO FULL VERSION