The private static final long serialversionuid that bothers you

Source: Internet
Author: User

A lot of times when a novice is writing code, your IDE will tell you a warning.

Then after you click on the warning, it will default to your class to generate a

Private static final long serialversionuid = 1L;

Then the obsessive-compulsive child never understood why it was needed, and what was this thing used for?

Then gradually you will find that this thing has nothing to do with the code you write, no eggs to use, so gradually you threw him aside.

This time I need to solve this problem, the private static final long Serialversionuid

First we need to know a knowledge point: What is Serializable?

This is an interface of Java that is used to complete serialization and deserialization operations.

So the question is, what is serialization and deserialization?

Any type that implements the serializable interface can be saved to a file or sent to another location as a data stream over the network. You can also use pipelines to transfer to other programs in your system.

This may not be clear enough, a little more straightforward.

Put an object in a way, packed and compressed into a string that makes it suitable for storing and transmitting, which is serialization.

Deserialization, which is to extract it from a string and return it to an object.

With this knowledge point, we come to the private static final long serialversionuid = 1L;

Why do you define this serialversionuid?

First we need to know that this is called the stream identifier, which is the version definition of the class, which can show that the declaration can also be implicitly declared.

Implicit declarations are computed by several factors, such as package name, class name, and so on. It's complicated.

This thing is simply a version name. It is used back when deserializing.

The JVM first compares this version name when deserializing, and if the serialversionuid in the data stream is the same as the Serialversionuid in the class, it will be deserialized, and different words will throw an exception. In general, if you modify a class that implements the serializable interface, you need to modify this version of the information.

If you show that the private static final long Serialversionuid = 1L is defined, then if you forget to modify this information and you modify the class, the class can be deserialized. And no error, a simple generalization is that if you forget to modify, then it will be version-compatible.

If you do not write the private static final long Serialversionuid = 1L, that is, implicitly declared, and you modify this class, then the version is incompatible, then there will be a case of deserialization error.

If you really do not understand, it doesn't matter, simply, remember to write the private static final long Serialversionuid = 1L, and then each time you modify the properties of the class, modify this value is OK.

And by the way: The controller in SPRINGMVC does not need to be declared, and the action in Struts2 is required, Seevlet is also required to be declared. As for why, need to look at the framework source code you can understand why this is the case. There is not much to say, in fact, I will not. If there is NB the great God can be commented on below.

The private static final long serialversionuid that bothers you

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.