The role of serialization Serialversionuid

Source: Internet
Author: User

in many applications, some objects need to be serialized to leave the memory space and stay on the physical hard disk for long-term storage. For example, the most common is the Web server session object, when there are 100,000 users concurrent access, there may be 100,000 session objects, memory may be unbearable, so the Web container will be some seesion first serialized to the hard disk, and so on, Then restore the object saved in the hard disk to memory, in plain enough, you can change a 2 binary file into an in-memory object. In Java, to implement this mechanism, as long as the implementation of the serializable interface can be, first look at the following simple example, Serialversionuid later. We first define a simple person class, then create this object and finally 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; }}/***** (serializes the 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 person= new Person ();   Person.setname ("Jack");                              ObjectOutputStream oo = new ObjectOutputStream (new FileOutputStream (New File ("E://jack.test")); Oo.writeobject (person);  Oo.close (); /***** (The object saved in the file can be restored to memory normally by the following method) *******/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 ("E:\\jack.test"));                Person person = (person) ois.readobject ();          String name= person.getname (); System.Out.Print ("name is:" +name);
everything went so well, but what if the person class changed after serialization? For example, one more member variable. Let's do the following experiment, or first serialize the object into a file, and then add a member variable to the person class, as follows:
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 run the restore again, we find that the operation error, will report the following error:
Exception in thread "main" Java.io.InvalidClassException:Person; local class Incompatible:stream Classdesc Serialve Rsionuid = 8383901821872620925, local class Serialversionuid = -763618247875550322
That is, the class in the file stream and the class in the classpath, that is, the modified class, is incompatible, in security considerations, the program throws an error, and refuses to load. < Span style= "line-height:19.5px" >       So what if we really have a need to add a field or method after serialization? What should I do? That is to appoint Serialversionuid. Before, in our case, we did not specify Serialversionuid, then the Java compiler will automatically give this Class A digest algorithm, similar to the fingerprint algorithm, as long as the file more than one space, the resulting UID will be very different, can be guaranteed in so many classes, This number is unique. So, after we added a field, the compiler generated a UID for us because we didn't explicitly specify Serialversionuid, and of course it won't be the same as the one previously saved in the file, so there's a 2-number inconsistent error. Therefore, as long as we specify the Serialversionuid, we can after serialization, to add a field, or method, without affecting the later restore, the restored object can still be used, but also more methods can be used, hehe. But how are we going to build serialversionuid? You can write 1, you can write 2, it doesn't matter, but it is best to follow the abstract algorithm, generate a unique fingerprint number, eclipse can be generated automatically, the JDK also comes with this tool. The general wording is similar to
Private static final long serialversionuid = -763618247875550322l; There is an exclamation point in front of the class that references serializable, click the exclamation mark and there will be a hint, one is the default, One automatically generates a serialversionuid! for this class


The role of serialization 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.