Functions of serialversionuid

Source: Internet
Author: User

 

In many applications, some objects need to be serialized so that they can leave the memory space and stay on the physical hard disk for long-term storage. For example, the most common session object in the web server,

When there are 0.1 million concurrent access users, there may be 0.1 million session objects, and the memory may not be enough. Therefore, the Web Container will serialize some seesion to the hard disk and use it later,

Then, restore the objects stored in the hard disk to the memory. To put it bluntly, a binary file can be converted into an object in the memory. To implement this mechanism in Java, you only need to implement the serializable interface.

Now, let's take a look at the simple example below. The serialversionuid will be introduced later. We first define a simple person class, then create this object, and then serialize it to a file.

 

/***** (Person class) ******/import java. Io. serializable;

Public class person implements serializable {private string name; Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}/****** (serialize an object to a file) *******/import Java. io. fileoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; public class whyserialversionuid {public static void main (string [] ARGs) throws exception {person = new person (); person. setname ("Jack"); objectoutputstream oo = new objectoutputstream (New fileoutputstream (new file ("E: // Jack. test "); oo. writeobject (person); oo. close ();}
/***** (The following methods can be used to restore the objects stored in the file to the memory) ******/import Java. io. fileoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream;
Public class whyserialversionuid {public static void main (string [] ARGs) throws exception {
Objectinputstream OIS = new objectinputstream (New fileinputstream (new file ("E: // Jack. test "); person = (person) Ois. readobject (); string name = person. getname (); system. out. print ("name is:" + name );
}

 

Everything went so smoothly, but what if the person class changed after serialization? For example, a member variable is added. We will do the following experiment to serialize the object to a file first,

Then add a member variable to the class "person", as shown below:

Import Java. io. serializable; public class person implements serializable {private string name; // Add such a member variable private string address; Public String getname () {return name;} public void setname (string name) {This. name = Name ;}}

 

After that, we can run the restore operation again to find an error. The following error will be reported:

Exception in thread "main" java. Io. invalidclassexception: person;

Local class incompatible: stream classdesc serialversionuid = 8383901821872620925, local class serialversionuid =-763618247875550322

This means that the class in the file stream is incompatible with the class in the classpath, that is, the modified class. In consideration of the security mechanism, the program throws an error and rejects loading.

So what if we really need to add a field or method after serialization? What should I do? That is, specify the serialversionuid. Previously, in our example,

We didn't specify the serialversionuid, so the Java compiler will automatically give this class A Digest algorithm, similar to the fingerprint algorithm, as long as the file has one more space, the obtained

UID will be completely different, and it can be ensured that this number is unique in so many categories. Therefore, after a field is added, because the serialversionuid is not explicitly specified

We generated a uid, which of course won't be the same as the one saved in the file, so two numbers are inconsistent. Therefore, as long as we specify the serialversionuid,

After serialization, you can add a field or method without affecting subsequent restoration. The restored object can still be used, and more methods can be used. However

How can we generate the serialversionuid? It doesn't matter if you can write 1 or 2, but you 'd better generate a unique fingerprint number based on the Digest algorithm,

Eclipse can be automatically generated, and JDK also comes with this tool. The general syntax is similar:

Private Static final long serialversionuid =-763618247875550322l;

There is an exclamation point in front of the class that references serializable. After you click this exclamation point, a prompt is displayed. One is the default one, and the other automatically generates a serialversionuid for this class!

 

 

 

Functions of serialversionuid

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.