Serialization in Java
Serialization in Java
Example:
be serialized. LinkedList and ArrayList choose to do so, but that is specific to their
implementation
Explain serialVersionUID ?
Whenever we declare any class implementing Serializable interface, then it always
prompts/warns to include/declare serialVersionUID which is of type long with modifier
final and static
Along with serialized object, this variable also gets serialized and during de-serialization both
serialVersionUID compared (stored serialVersionUID & value present inside declared class)
and then it converted back
If there is any mismatch, then InvalidClassException is thrown
Note: any changes to class results in change of serialVersionUID
Therefore, it is highly recommended to declare this value instead allowing compiler to
generate for us
What will happen if one of the members in the class doesn't implement serializable
interface?
Question: What happens if one of the members in a class does not implement
Serializable interface? Answer: When you try to serialize an object
which implements Serializable interface, incase if the object includes a reference
of an non serializable object then NotSerializableException will be thrown.
Serialization affects the very basics of Singleton design pattern, how can we
overcome to stop class to be serialized ?
Whenever Object is serialized and then again de-serialized then new object is created which
breaks basic principle of Singleton class
Serialization in java
https://www.benchresources.net/serialization-interview-question-and-answer-in-java/
Whether order of serialization & de-serialization need to be same? What
happens, if order is missed ?
Order of Serializationis very important to know, because we need to follow same
order while de-serializing objects
If Order of Serialization is unknown, then it may
throw java.lang.ClassCastException
To overcome ClassCastException, we can 1st check type of object
using instanceOf operator and then assign it to proper class after doing
necessary type-casting
Exception: iterating through while loop may throw EOFException, we need catch this
exception and handle it properly
Serialization process:
While serializing sub-class, JVM will check if there are any super-class which isn’t
implementing java.io.Serializable interface
Then, inheriting instance variables of non-serializable super-class will be stored
to default value ignoring their original values
Like 0 for Integer, null for String, etc
De-Serialization process:
While de-serializing sub class, JVM will check if there are any non-serializable super-
class
Then, it will execute instance initialization flow (i.e.; similar to object
instantiation flow)
Serialization in java
Why static member variables are not part of java serialization process
(Important)?
Answer. Serialization is applicable on objects or primitive data types only, but static
members are class level variables, therefore, different object’s of same class have same
value for static member.
So, serializing static member will consume unnecessary space and time.
Also, if modification is made in static member by any of the object, it won’t be in sync with other
serialized object’s value.
Note: When a class has instance fields which are serializable and
extendable, there is a caution you should be aware of. If the class has
invariants that would be violated if its instance fields were initialized to
their default values (zero for integral types, false for boolean, and null
for object reference types)
Add fields
Change a field from static to non-static
Change a field from transient to non-transient
Add classes to the object tree
Delete fields
Change class hierarchy
Change non-static to static
Change non-transient to transient
Change type of a primitive field
Serialization in java
How can one customize the Serialization process? or What is the purpose of
implementing the writeObject() and readObject() method?
Ans) When you want to store the transient variables state as a part of the serialized
object at the time of serialization the class must implement the following methods –
TransientStaticVariable .java
package com.learnfromexamples.transientvsstatic;
import java.io.Serializable;
public TransientStaticVariable() {
System.out.println("Constructorr Called...");
}
public transient String variableOne;
public transient String variableTwo = "V2";
public static String variableThree;
Serialization in java
Serialization.java
package com.learnfromexamples.transientvsstatic;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
}
Output
Constructorr Called...
Deserialization.java
package com.learnfromexamples.transientvsstatic;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
}
Output
After Deserialization...
null
null
null
V4
null
V6
V7
V8