zoukankan      html  css  js  c++  java
  • What is a serialVersionUID and why should I use it?

    RT.

    今天搜的,记录下来,给自己补补脑

    http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it

    里面有一篇blog,原文地址http://www.javablogging.com/what-is-serialversionuid/

    What is serialVersionUID?

    Most people learn about serialVersionUID after they write their first serializable object (I know I did). You add ‘implements Serializable’ and in the next moment your IDE starts complaining… so what’s up?

    Lets look at a simple example to see what meaning that variable has. In the example we will use the class SerializeMe shown below:

     1 class SerializeMe implements Serializable {
     2     private static final long serialVersionUID = 1L;
     3 
     4     private int data;
     5 
     6     public SerializeMe (int data) {
     7         this.data = data;
     8     }
     9 
    10     public int getData() {
    11         return data;
    12     }
    13 }

    Field data represents some information stored in the class. Class implements the Serializable interface, so my IDE automatically offered me to declare the serialVersionUID field. Lets start with value 1 set there.

    We will also need some class that will serialize and deserialize the SerializeMe class. Here it is:

     1 public class UIDTester {
     2     public static void main(String... strings) throws Exception {
     3         File file = new File("out.ser");
     4         FileOutputStream fos = new FileOutputStream(file);
     5         ObjectOutputStream oos = new ObjectOutputStream(fos);
     6 
     7         SerializeMe serializeMe = new SerializeMe(1);
     8         oos.writeObject(serializeMe);
     9         oos.close();
    10 
    11         FileInputStream fis = new FileInputStream(file);
    12         ObjectInputStream ois = new ObjectInputStream(fis);
    13 
    14         SerializeMe dto = (SerializeMe) ois.readObject();
    15         System.out.println("data : " + dto.getData());
    16         ois.close();
    17     }
    18 }

    This code will serialize an instance of class SerializeMe to a file and then deserialize it back. Now let’s run the main function! You should get output that says:

    data : 1

    It means that our object was properly serialized and deserialized. Note that the file “out.ser” was created on disk and it is still there even after the program finished. Let’s see if we can read that file once again, this time without creating it first. To do that, comment out lines from 4 to 9 in the UIDTester class:

     1 public class UIDTester {
     2     public static void main(String... strings) throws Exception {
     3         File file = new File("out.ser");
     4         //FileOutputStream fos = new FileOutputStream(file);
     5         //ObjectOutputStream oos = new ObjectOutputStream(fos);
     6 
     7         //SerializeMe serializeMe = new SerializeMe(1);
     8         //oos.writeObject(serializeMe);
     9         //oos.close();
    10 
    11         FileInputStream fis = new FileInputStream(file);
    12         ObjectInputStream ois = new ObjectInputStream(fis);
    13 
    14         SerializeMe dto = (SerializeMe) ois.readObject();
    15         System.out.println("data : " + dto.getData());
    16         ois.close();
    17     }
    18 }

    This way, the main method starts right away with reading the file from the disk and deserializing data stored in it. As the file is exactly the same and our class didn’t changed either, we should get exactly the same output:

    data : 1

    Now, let’s see what happens, when we change the serialVersionUID value and try to deserialize once again our file. Change the line 2 in the class SerializeMe so that serialVersionUID contains now 2 instead of 1:

    1 private static final long serialVersionUID = 2L;

    Now let’s run again our program (just like one step before, with commented out lines writing the file). We should get an exception like this:

    Exception in thread "main" java.io.InvalidClassException:
    SerializeMe; local class incompatible: stream classdesc
    serialVersionUID = 1, local class serialVersionUID = 2

    As you can see, this time the deserialization didn’t go well. ObjectInputStream complained about the serialVersionUID being changed. How does he know that it changed? If serialVersinUID is static, then it should not have been serialized in the first place, and there should be no information about the previous value 1 during the deserialization, right? Well, serialVersionUID is an exception to the rule that “static fields don’t get serialized”ObjectOutputStream writes every time the value ofserialVersionUID to the output stream. ObjectInputStream reads it back and if the value read from the stream does not agree with the serialVersionUID value in the current version of the class, then it throws the InvalidClassException. Moreover, if there is no serialVersionUID officially declared in the class to be serialized, compiler automatically adds it with a value generated based on the fields declared in the class.

    So what is it for after all? Let’s suppose that there is some file storing a serialized object of some class A. The deserialization of that object does not necessarily have to occur exactly after serialization. It can occur after a few months or on a completely different JVM (i.e. sending an object through net using serialization). In both cases, there is a chance that the class declaration has changed between serialization and deserialization. It would be nice to have some kind of versioning system for every serializable class – and serialVersionUID does exactly that. It checks if the data read from the input stream is compatible with the current definition of the class.

    If so, then why is it recommended to specify your own serialVersionUID ? It gives us simply more control. Default rules for generating serialVersionUID can be too strict in some cases. For example when the visibility of a field changes, the serialVersionUID changes too. Value generated automatically can differ between various Java implementations. There is a chance that some object serialized on one Java implementation will not deserialize on some other Java implementation, even if the class definition is exactly the same. Furthermore, sometimes you just want for some reason to forbid deserialization of old serialized objects, and in this case you just have to change the serialVersionUID.

    Now that you know that specifying your own serialVersionUID is recommended, you might tend to write it once for every serializable class ( or have it generated by the IDE ) and forget about it. WRONG !!! If you write it once and don’t take care to update it when necessary, you lose all the merits of serialVersionUID. All your classes will get deserialized without exceptions even when using outdated data. What you should do is to change serialVersionUID (for example increase it by 1 or make your IDE generate automatically a new value) every time there is some change in the definition of data stored in the class. For example if you change data types, variable names or add new data – hence every time you want to have ‘backward incompatibility’ for deserialization.

  • 相关阅读:
    javascript 模板系统 ejs v2
    三国观后感
    《非诚勿扰》乐嘉老师送给男生女生的话:
    正则学习笔记6
    硬链接和符号链接
    javascript 模板系统 ejs v1
    javascript模板系统 ejs v3
    程序员编程艺术:第八章、从头至尾漫谈虚函数
    程序员编程艺术第十一章:最长公共子序列(LCS)问题
    编程艺术第二十三~四章&十一续:杨氏矩阵查找,倒排索引关键词Hash编码
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3127647.html
Copyright © 2011-2022 走看看