Serialization and Deserialization in Java

Serialization in Java is a sophisticated mechanism that involves meticulously inscribing the intricate state of an object into a sequence of bytes, thereby facilitating its preservation and transfer. On the other hand, deserialization serves as a meticulous process of skillfully transmuting a stream of bytes back into an impeccably replicated incarnation of the original object.

For a Java object to be deemed serializable, it must fulfill a specific criterion: either its class or any of its superclasses must implement either the esteemed java.io.Serializable interface or its distinguished subinterface, java.io.Externalizable. This criterion ensures that the object possesses the essential qualities necessary for successful serialization and deserialization operations.

Marker Interface

The Serializable interface serves as a distinctive "marker" interface in Java. It operates without any explicit methods or fields but instead acts as a symbolic indication that a particular class possesses the capability of being serialized. When the Java Virtual Machine (JVM) encounters a class that bears the Serializable marker during the serialization process, it automatically recognizes it as suitable for writing to the stream with utmost confidence and security. This streamlined process alleviates much of the burden from the programmer, allowing for seamless and efficient serialization operations. Following are the well-known Marker Interfaces.

  1. rmi.Remote
  2. io.Serializable
  3. lang.Cloneable
Example
import java.io.*; class Student implements Serializable { int id; String name; Student(int id, String name) { this.id = id; this.name = name; } }
Serializing an Object
public class TestClass{ public static void main(String[] args) { try{ Student st = new Student(101,"John"); FileOutputStream fos = new FileOutputStream("student.info"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(st); oos.close(); fos.close(); }catch(Exception e){ System.out.println(e); } } }
Deserialization of Object
public class TestClass{ public static void main(String[] args) { Student st = null; try{ FileInputStream fis = new FileInputStream("student.info"); ObjectInputStream ois = new ObjectInputStream(fis); st = (Student)ois.readObject(); }catch(Exception e){ System.out.println(e); } System.out.println(st.id); System.out. println(st.name); } }

Serialization and Variables

Instance Variables:

By marking variables as serializable, we ensure that their states are preserved during the serialization process. Consequently, when we perform deserialization, we can retrieve the serialized state of these variables, restoring them to their previous state. This allows for the seamless transfer and reconstruction of object data, maintaining consistency and coherence throughout the serialization and deserialization operations.

Static Variables:

If the variables in question are not marked as serializable, they will not undergo the serialization process. As a result, during deserialization, the static variable values will not be restored from the serialized state. Instead, the static variables will be loaded from the class itself.

It's important to note that any static variable that has been assigned a value during class initialization will be serialized by default. However, in typical scenarios where you provide the value to a static variable at runtime, such as in the main class, this value will not be serialized. The serialized state of an object usually focuses on instance-specific data, while static variables are associated with the class as a whole and are not part of the serialized state in most cases.

Therefore, it's crucial to consider the distinction between instance variables and static variables when dealing with serialization and deserialization. Instance variables are typically the ones that are serialized and restored during the deserialization process, while static variables retain their values from the class itself.

Transient Variables:

Transient variables in Java are not serialized. When an object is serialized, any transient variables within that object are not included in the serialization process.

During deserialization, when the object is reconstructed from the serialized form, the transient variables are not restored to their previous values. Instead, they are initialized with their corresponding default values based on their data types. For example, transient variables of numeric types are initialized to 0, boolean variables are initialized to false, and object references are initialized to null.

This behavior allows you to exclude specific variables from being serialized, which can be useful for sensitive data or variables that are not relevant to the serialized form of the object. By marking variables as transient, you have control over which variables are persisted during serialization and which ones are excluded from the serialization process.

Super class variables:

When a subclass implements the Serializable interface and its superclass also implements Serializable, the variables of both the subclass and superclass will be serialized. However, if the superclass does not implement Serializable, the superclass variables will not be serialized.

During the deserialization process, the Java Virtual Machine (JVM) ensures that the objects are correctly reconstructed. It does this by invoking the default constructor of the superclass and populating the default values for its variables. This process is repeated for each superclass in the inheritance hierarchy, ensuring that all variables, including those in superclasses, are appropriately initialized.

It's important to note that in order for the superclass variables to be correctly deserialized, the superclass itself must also implement Serializable. If the superclass does not implement Serializable, its variables will not be part of the serialization and deserialization process, potentially leading to inconsistent or incomplete object reconstruction.

Conclsuion

Serialization and deserialization in Java involve the process of converting an object into a stream of bytes for storage or transmission, and then restoring the object from that stream to its original state, respectively.