If a class implements serializable occurs, eclipse will prompt to generate the serialversionuid. In most cases, I choose yes. But what is this thing?
package com.home.demo;import java.io.Serializable;public class Item implements Serializable {private static final long serialVersionUID = 1L;private int data; public Item (int data) { this.data = data; } public int getData() { return data; }}
Eclipse again reminds me to have serialversionuid. Okay, since it's a test, give it a value of 1.
Write a test class and try serialize and deserialize item
package com.home.demo;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class UIDTest {public void save2Disk(String fileName) throws IOException{File file = new File(fileName); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); //set value to 101 Item serializeMe = new Item(101); oos.writeObject(serializeMe); oos.close(); System.out.println("done!");}public void readFromDisk(String fileName) throws ClassNotFoundException, IOException{FileInputStream fis = new FileInputStream(fileName); ObjectInputStream ois = new ObjectInputStream(fis); Item dto = (Item) ois.readObject(); System.out.println("data : " + dto.getData()); ois.close();}/** * @param args * @throws IOException * @throws ClassNotFoundException */public static void main(String[] args) {UIDTest test = new UIDTest();String name = "D:/out.dat";try{// write file//test.save2Disk(name); //--1test.readFromDisk(name); // --2}catch(Exception e){e.printStackTrace();} }}
Running result
101
Completely correct!
Look at drive D. There is a file out. dat. Close the Write File and directly read out. dat.
Running result
101
TrySerialversionuid = 2L
private static final long serialVersionUID = 2L;
Run again
java.io.InvalidClassException: com.home.demo.Item; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
Oh .... Error!
According to Java document,
Serialization is for serializing instances, not classes. static fields (methods are irrelevant since they are part of the class definition so they aren't serialized) will be reinitialized to whatever value they are set to when the class is loaded.
According to this statement, item cannot be known.SerialversionuidWhy is an error reported?
Continue reading the document!
Okay, the serialversionuid is an exception. This value is indeed "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.
If you have serialize data on machine A and then sent it to the deserialize on machine B, you need some information to help determine the correctness of the data. Serialversionuid is used for this activity. Therefore, we recommend that you use the serialversionuid for testing and controlling serialization.
How can I use serialversionuid correctly?
What you shoshould 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.
Reference
Http://stackoverflow.com/questions/6429462/java-static-serialization-rules
Http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
Http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it
Http://www.javablogging.com/what-is-serialversionuid/
Http://www.mkyong.com/java-best-practices/understand-the-serialversionuid/
Serializable and serialversionuid