Serialversionuid in Java

Source: Internet
Author: User

Serialversionuid in Java

Serialization and Sergalversionuid plague many Java developers. I often see this problem, what is serialversionuid, what if SERIALVERSIONUID is not defined in the class that implements the Serializable interface? Aside from its complexity and its less common use, one of the reasons is that eclipse has a warning prompt after missing serialversionuid: "The Serializable class Customer does not declare a static Final serialversionuid field of type Long ". From this article you can learn the basics of serialversionuid and the role it plays in serialization and deserialization. When you declare a class to be serializable by implementing the Java.io.Serializable interface, and you do not customize your own serialization by using the Externalizable interface, Java uses the default serialization mechanism to persist the class to disk at run time. During serialization, Java generates a version number for this class at run time so that it can be deserialized later. This version number is serialversionuid. If Serialversionuid does not match during deserialization, this deserialization process will fail. The java.io.InvalidClassException exception is thrown and the name of the class and the corresponding serialversionuid are printed. A quick solution is to declare a private static final long type Serialversionuid constant in your class. In this article you will learn why you should use Serialversionuid and how to use the JDK tool Serialver to generate this ID. If you never know about serialization, you can look at the 10-side questions in the Java serialization to test your knowledge and see if it's difficult to understand what you're reading down there. Like concurrency and multithreading, serialization is another topic that deserves to be read over and over again.

Why use Serialversionuid in Java

As I said earlier, if your class does not declare a static, final, and long type of Serialversionuid property, the Java serialization mechanism will generate one for you. Its generating mechanism is influenced by many factors, including the fields in the class, the access limiter, the interface of the class implementation, and even the different compiler implementations, any class modification or the use of different compiler generated serialversionuid are not the same, It is likely that eventually the reloading of serialized data is aborted. It is dangerous to rely on the Java serialization mechanism to generate this ID, which is why it is recommended that you declare a serialversionuid yourself in a class that needs to be serialized. I highly recommend that you read the Java classics of Joshua Bloch, effective Java to understand the problems caused by the serialization mechanism of Java and the handling of errors. Incidentally, the JDK provides a tool called Serailver, which is in the bin directory under the Javahome folder, on my machine: C:\Program files\java\jdk1.6.026\bin\ Serialver.exe, you can use it to generate SERIALVERSIONUID for old class files. This tool is useful if you modify your class to break the serialization process and cause your application to reload the serialized data. You can use this tool to regenerate serialversionuid for an old instance, and then explicitly specify it by declaring a static final long type of serialversionuid in your class. At the same time, it is highly recommended to use a custom binary format for serialization, whether for performance or security reasons, which has been mentioned many times in effective Java, with a detailed introduction to the benefits of custom formatting.

How to use the JDK tool serialver to generate Serialversionuid

You can use the serialver of the JDK to generate the serialversionuid of the class. This is especially useful for existing classes, and the serialversionuid that it returns is well suited for replication use. You can use serialver like this in the following 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 run $serialver-show to use the Serailver tool in the form of a GUI, which opens a viewer for the serialized version number, which receives the full class name and outputs the corresponding serialized version number.

Summarize

Now that we know what serialversionuid is and why it is so important to declare serialversionuid in a class, you can review some of the important concepts that are relevant.

The serialversionuid is used to serialize data. Only if the serialversionuid of the serialized instance and the current class match can be deserialized. If you do not declare serialversionuid in the class, Java will generate one for you at run time, but the resulting process will be affected by such factors as the number of fields, the field type, the access restrictions of the field, and the interfaces implemented by the class. In Oralce's official documentation you can find accurate descriptions of the serialization. It is recommended that you declare privae static final long type serialversionuid field to avoid the default mechanism. If you don't, some IDE like eclipse will prompt a warning message: "The Serializable class Customer does not declare a static final serialversionuid field of Type Long ". Although you can block this feature through window > Preferences > Java > Compiler > Errors/warnings > Potential programming problems , but I suggest you don't do it. The only exception I've ever seen is when you don't need to recover the data. Here's the bug in the Eclipse IDE, all you need to do is click Quick Fix.

You can use the Serailver tool provided by the JDK to generate a sequence version number for the Java class. It also has a GUI interface that can be enabled by passing the-show parameter. The best practice for Java serialization is to explicitly declare serialversionuid to avoid problems that may arise during deserialization, especially if the application of the C/s mode you are running relies on RMI for data serialization.

This is the entire contents of Serialversionuid in Java. Now we know the importance of declaring serialversionuid in a class. Thank you for the IDE for giving you this hint, or your class might be having problems deserializing.

If you want to know more about serialization related knowledge, you can refer to the following several good articles:

Differences between transient and volatile variables in Java when transient variables should be used in different Java serializable and Externalizable interfaces

Serialversionuid in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.