Serialization
What is serialization?
Serialization is the process of conversion of object into stream that can be sent over network or save it as a file or store in database as blob for later usage

What is the use of Serialization?
As written above serialization will translate the Object state to Byte Streams. This Byte stream can be used for different purpose.
ü Write to Disk
ü Store in Memory
ü Sent byte stream to other platform over network
ü Save byte stream in DB(As BLOB)

Along with serialization we hear another term associate with it that is Deserialization 

What is Deserialization ?
Deserialization is opposite to serialization. It is the process of converting Object stream to actual Java Object to be used in our program.

How can we serialize a class object?
ü In order to serialize a class we need to implement “java.io.Serializableinterface ”
ü Serializable Interface is a Marker Interface.
ü Serialization process is implemented by ObjectInputStream and ObjectOutputStream
ü While Serializing if you don’t want some of the member to serialize we can user transient  or static keyword, variables declared as transient or static will not be serialized (to know more about transient variable click here)

Serialized class will have serialVersionUID if we don’t define it, During serialization, Java runtime creates a version number for a class, so that it can de-serialize it later. This version number is known as serialVersionUID in Java (you can see what is serialVersionUID here)
There are four methods that we can provide in the class to change the      serialization behavior.
If these methods are present in the class, they are used for serialization purposes.
1. readObject(ObjectInputStream ois): If this method is present in the class, ObjectInputStream readObject() method will use this method for reading the object from stream.
2. writeObject(ObjectOutputStream oos): If this method is present in the class, ObjectOutputStream writeObject() method will use this method for writing the object to stream. One of the common usage is to obscure the object variables to maintain data integrity.
3. Object writeReplace(): If this method is present, then after serialization process this method is called and the object returned is serialized to the stream.
4. Object readResolve(): If this method is present, then after deserialization process, this method is called to return the final object to the caller program. One of the usage of this method is to implement Singleton pattern with Serialized classes.

Note:
ü If Super class implements Serializable then sub class are also Serializable automatically.
ü If Super class is not Serializable then when sub class is Deserialized then super class’s default constructor will be invoked. Hence all variable will get default value and reference will be null


What is serialVersionUID in java:
The serialVersionUID is used as a version control in a Serializable class. If you do not explicitly declare a serialVersionUID, JVM will do it for you automatically, based on various aspects of your Serializable class

If  no serialVersionUID is declared, JVM will use its own algorithm to generate a default serialVersionUID, you can check the algorithm here.The default serialVersionUID computation is highly sensitive to class details and may vary from different JVM implementation, and result in an unexpected InvalidClassExceptions during the Deserialization process.

If during Deserialization, serialVersionUID doesn't match than process will fail with InvalidClassException as  Exception in thread "main" java.io.InvalidClassException, also printing class-name and respective serialVersionUID.

One quick solution to fix this problem is copying serialVersionUID and declaring them as private static final long constant in your class. In this article, we will learn about why we should use serialVersionUID in Java and How to use serialver JDK tool for generating this ID.

No comments:

Post a Comment