Java
Module 20: SerializationBeginner20 min read

Object Serialization, transient & serialVersionUID

Understand Java serialization, excluding sensitive fields with transient, and versioning classes with serialVersionUID.

Key Takeaways & Core Concepts
  • `Serializable`: A marker interface (no methods). If a Serializable class holds a reference to a non-serializable field, serialization throws NotSerializableException unless that field is marked transient.
  • `serialVersionUID`: Always declare private static final long serialVersionUID = 1L;. If omitted, the JVM computes a hash from class structure—meaning adding a single method breaks deserialization of previously saved objects with InvalidClassException.
  • `transient` & `static`: Both transient fields and static fields are skipped during serialization.
  • Security Warning: Never deserialize untrusted byte streams from external users (deserialization gadget attacks); prefer JSON (Jackson/Gson) or Protobuf for APIs.

Introduction

Serialization converts an in-memory Java object graph into a portable byte stream (ObjectOutputStream) for disk persistence or network transfer; Deserialization reconstructs the object graph (ObjectInputStream).

Serialization & Transient Field Protection
1import java.io.*;
2
3class SessionUser implements Serializable {
4 private static final long serialVersionUID = 1L;
5 String username;
6 transient String secretToken;
7
8 SessionUser(String u, String t) { username = u; secretToken = t; }
9}
10
11public class Main {
12 public static void main(String[] args) throws Exception {
13 ByteArrayOutputStream baos = new ByteArrayOutputStream();
14 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
15 oos.writeObject(new SessionUser("ashutosh", "secret-123"));
16 }
17 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
18 SessionUser restored = (SessionUser) ois.readObject();
19 System.out.println("User: " + restored.username + ", Token: " + restored.secretToken);
20 }
21 }
22}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
22 lines
Note: `secretToken` is omitted from the serialized stream and restored as `null`.

Key Mechanics & Rules

  • `Serializable`: A marker interface (no methods). If a Serializable class holds a reference to a non-serializable field, serialization throws NotSerializableException unless that field is marked transient.
  • `serialVersionUID`: Always declare private static final long serialVersionUID = 1L;. If omitted, the JVM computes a hash from class structure—meaning adding a single method breaks deserialization of previously saved objects with InvalidClassException.
  • `transient` & `static`: Both transient fields and static fields are skipped during serialization.
  • Security Warning: Never deserialize untrusted byte streams from external users (deserialization gadget attacks); prefer JSON (Jackson/Gson) or Protobuf for APIs.
Practice & AssessmentJava Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)