Introduction:
Serialization and deserialization in Java is the process of converting an object’s state into a byte stream, which can then be saved to a file or transferred over a network. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory.
Why is Serialization in Java Needed?
Serialization in java allows objects to be stored and transferred between different environments while maintaining their state. Since this objects exist only in memory during program execution, serialization provides a way to persist and retrieve object states when needed.

1. Key Benefits:
Persistence: Save object state for future use.
Interoperability: Serialized objects can be transferred over a network and reconstructed on different platforms.
Caching: Object states can be cached and retrieved later.
RMI(Remote Method Invocation): Used in distributed applications to send objects between Java programs.
2. Java Serializable Interface
To make an object serializable, the class must implement the java.io.Serializable interface. This is a marker interface, meaning it does not contain any method but is used to indicate that a class is serializable.
Example of a Serializable Class:
import java.io.*;
class Employee implements Serializable{
private static final long serialVersionUID = 1L;
String name;
int age;
public Employee(String name, int age){
this.name=name;
this.age=age;
}
}
3. Serialization Process
Serialization is performed using the ObjectOutputStream class, which writes the object into a file as a byte stream.
Syntax of writeObject Method:
public final void writeObject(Object obj) throws IOException;
Serialization Example:
import java.io.*;
public class SerializationExample{
public static void main(String[]args){
Employee emp=new Employee(“John Doe”,30);
String filename=”employee.ser”‘
try{
FileOutputStream file=new FileOutputStream(filename);
ObjectOutputStream out= new ObjectOutputStream(file);
out.close();
file.close();
System.out.println(“Object has been serialized”);
} catch(IOException){
System.out.println(“IOException is caught”);
}
}
4. Deserialization Process
Deserialization is the process of reading an object from a file and reconstruction it in memory.
Syntax of readObject Method:
public final Object readObject() throws IOException,ClassNotFoundException;
Deserialization Example:
import java.io.*;
public class DeserializationExample{
public static void main (String[]args){
Employee emp=null;
String filename=”employee.ser”;
try{
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in= new ObjectInputStream (file);
emp = (Employee) in.readObject();
in.close();
file.close();
System.out.println(” Object has been deserialized”);
System.out.println(“Name: ” + emp.name);
}catch( IOException | ClassNotFoundException ex){
System.out.println(“Exception is caught”);
}
}
}
5. Important Points About Serialization
If a parent class implements Serializable, then the child does not need to explicitly implement it. Static fields are not serialized because they belong to the class, not the object. Transient fields are not serialized and will be initialized with default values when deserialized. The constructor of the class is not called during Deserialization.
Associated objects must also implement Serializable, otherwise NotSerializableException will be thrown.
6. Transient Keyword in Serialization
If you don’t want a field to be serialized, mark it as transient. This is useful for sensitive information like passwords.
Example Using transient Keyword:
import java.io.*;
class User implements Serializable{
private static final long serialVersionUID = 1L;
String username;
transient String password;
public User(String username, String password){
this.username= username;
this.password=password;
}
}
Deserialization Output:
username will be restored.
password will be null because it was marked transient.
7. SerialVersionUID
Each serializable class should have a serialVersionUID to ensure that the same version of the class is used during serialization and deserialization. If different versions are used, InvalidClassException will be thrown.
Declaring SerialVersionUID:
private static final long serialVersionUID=42L;
Getting SerialVersionUID Using serialver Tool:
serialver -classpath.Employee
8. Impact of static and transient in Serialization
Effect of transient and static Variables in Serialization:
import java.io.*;
class Emp implements Serializable{
private static final long serialVersionUID = 129348938L;
transient int a ;
static int b;
String name;
int age;
public Emp(String name, int age, int a, int b){
this.name=name;
this.age=age;
this.a=a;
Emp.b=b;
}
}
Output Explanation:
a is transient , so it becomes 0 after deserialization.
b is static, so its latest value in memory is used after deserialization.
9. Final vs Transient in Serialization
Final variables are serialized with their values because they are constants.
Example Usign final and transient:
import java.io.*;
class Dog implements Serializable{
int i=10;
transient final int j=20;
}
Output:
Serialization started
Serialization ended
Deserialization started
Deserialization ended
Dog object data
10 20
final variables retain their values even when marked transient.
10. Conclusion
Serialization and deserialization in java is a powerful feature in Java for object persistence and data transmission. Understanding it’s nuances, including transient, static, and serialVersionUID, is essential for writing efficient and reliable Java applications.
Proper handling of serialization ensures data integrity and compatibility across different platforms and versions.
Check out more Blogs – Click here
Visit LinkedIn page – Click Here