For the clear understanding of this concept you need to be aware of the Serialization and Deserialization concepts of Java.
What we have in Serialization as an overview is:
1)The class which needs to be traveled on network and to be saved anywhere ,must implement the java.io.Serializable interface.
2)All of the fields in that class must be Serializable. If a field is not Serializable, it must be marked transient so that compiler comes to know that this field need not to be saved.
3) The ObjectOutputStream class is responsible to serialize an Object.
4) A file named Engineer.ser is created after the serialization is completed (the class name is Engineer.java).
Following lines of code might be used as a summary:
FileOutputStream fileOut = new FileOutputStream("/tmp/Engineer.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e); // e is the instance of the class Engineer.java
out.close();
fileOut.close();
Hence , writeObject is responsible method to serialize the object.Here is magic of serialversionUID takes place. Keep going for the suspense.
On the other hand, when we Deserialize the object back and convert it to the object itself then the readObject method comes into ground.
The signatures of these two methods are as follows:
public final void writeObject(Object x) throws IOException
public final Object readObject() throws IOException, ClassNotFoundException
Now you might be clear about the usage of writeObject and readObject methods , lets proceed towards the concept of serialversionUID.
Every class which we are going to serialize must have a serialVersionUID . This is nothing but Universal Version Identifier. This is used when we Deserialize the class. It checks on the time of Deserialization that whether it is the same class or modified one. It matches up the serialversionUID which was created on the time of serialization. If it does not see the same serialVersionUID then it throws the InvalidClassException.
If you don't provide the serialversionUID to the class then it will create its own.
For example, if you serialize an object of Class Engineer version 1 (E1) which doesn't have a serialversionUID, then you modify it to version 2 (E2) and save it, you'll have two saved files, one with E1 and one with E2 objects. At this point no matter what serial version ID you put in the class, you will be unable to Deserialize one of the saved files.
Hence, if you will put the serialversionUID then there will not be any problem because Deserialization process will get the same serialversionUId all the times and would come to know that this is the same class which was serialized.
How to compute a serialVersionUID ?
There is a serialver tool available if we need to compute the serialVersionUID at any time. It is inbuilt with JDK.
In our case class is Engineer.java and suppose it is in the com.abc package then the computation of serialVersionUID would be like:
1) Open the console.
2) Put the following code and press enter.
serialver com.abc.Engineer
and you'll get output like this:
com.abc.Engineer: static final long serialVersionUID = -8718469741124325816L;
You can take the code starting with "static" and place it inside your class with other static variables. Now the serial version ID is locked in your class.
What we have in Serialization as an overview is:
1)The class which needs to be traveled on network and to be saved anywhere ,must implement the java.io.Serializable interface.
2)All of the fields in that class must be Serializable. If a field is not Serializable, it must be marked transient so that compiler comes to know that this field need not to be saved.
3) The ObjectOutputStream class is responsible to serialize an Object.
4) A file named Engineer.ser is created after the serialization is completed (the class name is Engineer.java).
Following lines of code might be used as a summary:
FileOutputStream fileOut = new FileOutputStream("/tmp/Engineer.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e); // e is the instance of the class Engineer.java
out.close();
fileOut.close();
Hence , writeObject is responsible method to serialize the object.Here is magic of serialversionUID takes place. Keep going for the suspense.
On the other hand, when we Deserialize the object back and convert it to the object itself then the readObject method comes into ground.
The signatures of these two methods are as follows:
public final void writeObject(Object x) throws IOException
public final Object readObject() throws IOException, ClassNotFoundException
Now you might be clear about the usage of writeObject and readObject methods , lets proceed towards the concept of serialversionUID.
Every class which we are going to serialize must have a serialVersionUID . This is nothing but Universal Version Identifier. This is used when we Deserialize the class. It checks on the time of Deserialization that whether it is the same class or modified one. It matches up the serialversionUID which was created on the time of serialization. If it does not see the same serialVersionUID then it throws the InvalidClassException.
If you don't provide the serialversionUID to the class then it will create its own.
For example, if you serialize an object of Class Engineer version 1 (E1) which doesn't have a serialversionUID, then you modify it to version 2 (E2) and save it, you'll have two saved files, one with E1 and one with E2 objects. At this point no matter what serial version ID you put in the class, you will be unable to Deserialize one of the saved files.
Hence, if you will put the serialversionUID then there will not be any problem because Deserialization process will get the same serialversionUId all the times and would come to know that this is the same class which was serialized.
How to compute a serialVersionUID ?
There is a serialver tool available if we need to compute the serialVersionUID at any time. It is inbuilt with JDK.
In our case class is Engineer.java and suppose it is in the com.abc package then the computation of serialVersionUID would be like:
1) Open the console.
2) Put the following code and press enter.
serialver com.abc.Engineer
and you'll get output like this:
com.abc.Engineer: static final long serialVersionUID = -8718469741124325816L;
You can take the code starting with "static" and place it inside your class with other static variables. Now the serial version ID is locked in your class.
No comments:
Post a Comment