Serialization in Java
Serialization is the process of converting a Java object's state into a byte stream so it can be saved to a file, sent over a network, or stored in a datab...
Converting Objects to Bytes and Back
Serialization is the process of converting a Java object's state into a byte stream so it can be saved to a file, sent over a network, or stored in a database. Deserialization is the reverse: reconstructing a Java object from that byte stream. Together, they allow objects to persist beyond the lifetime of a program or travel across a network to a different JVM instance. Java's built-in serialization mechanism is provided by ObjectOutputStream and ObjectInputStream.
Making a Class Serializable A class participates in Java's built-in serialization by implementing the java.io.Serializable interface. Serializable is a marker interface—it has no methods. Its presence tells the JVM that this class's objects may be serialized. All non-transient, non-static fields are included in the serialized form. Fields marked transient are intentionally excluded, which is useful for sensitive data like passwords or for fields that cannot be serialized (like database connections or open file handles).
The serialVersionUID Field When the JVM serializes an object, it records the class's serialVersionUID—a long value that acts as a version stamp. During deserialization, the JVM compares the stamp in the byte stream with the stamp in the current class. A mismatch causes an InvalidClassException. If you add or remove fields from a class, the auto-generated UID changes and old serialized data becomes incompatible. Declaring serialVersionUID explicitly in your class gives you control over compatibility: you decide when a version change is breaking.
Alternatives to Built-In Serialization Java's built-in serialization has significant drawbacks: it is Java-specific (cannot be read by other languages), the format is fragile (field names and types are baked in), and it has had serious security vulnerabilities where malicious streams could execute arbitrary code during deserialization. Modern systems use JSON (Jackson, Gson), XML, Protocol Buffers, or Avro for serialization. These formats are language-neutral, human-readable or compact, and safer. Built-in Java serialization is now rarely used in new code.
Code Example: import java.io.*;
class Employee implements Serializable { private static final long serialVersionUID = 1L; String name; transient String password; // excluded from serialization Employee(String name, String password) { this.name = name; this.password = password; } }
// Serialize try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("emp.dat"))) { oos.writeObject(new Employee("Alice", "secret")); }
// Deserialize try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("emp.dat"))) { Employee e = (Employee) ois.readObject(); System.out.println(e.name); // Alice System.out.println(e.password); // null (transient) }
Interview Tip: When asked about Java serialization, mention security first. Deserializing data from an untrusted source can execute arbitrary code through gadget chains—this attack class caused major vulnerabilities in Java enterprise applications. The safe practice is to never deserialize data from untrusted sources and to use an ObjectInputFilter (added in Java 9) or a modern serialization library with explicit class allowlists.