← Back to Core Java Course | Chapter 12: File I/O | Lesson 8 of 9

Java Serialization

What is Serialization?

Serialization converts a live Java object into a byte stream that can be saved to a file or sent over a network, and later reconstructed back into an equivalent object — the class must implement the marker interface Serializable.

Example: What is Serialization?

java
import java.io.Serializable;
class Person implements Serializable {
	String name = "Alice";
}
public class Main {
	public static void main(String[] args) {
		Person p = new Person();
		System.out.println(p instanceof Serializable);
	}
}

Writing Objects with ObjectOutputStream

ObjectOutputStream writes a serializable object's full state to an underlying stream via writeObject(), capturing every non-transient field's current value.

Example: Writing Objects with ObjectOutputStream

java
import java.io.*;
class Person implements Serializable {
	String name = "Alice";
}
public class Main {
	public static void main(String[] args) throws IOException {
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"));
		out.writeObject(new Person());
		out.close();
		System.out.println("Written");
	}
}

Reading Objects with ObjectInputStream

ObjectInputStream reads that byte stream back and reconstructs the object via readObject(), restoring its fields to the values they had at serialization time.

Example: Reading Objects with ObjectInputStream

java
import java.io.*;
class Person implements Serializable {
	String name = "Alice";
}
public class Main {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"));
		out.writeObject(new Person());
		out.close();
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"));
		Person p = (Person) in.readObject();
		in.close();
		System.out.println(p.name);
	}
}

Transient Fields

Marking a field transient excludes it from serialization entirely — commonly used for sensitive data like passwords, or for fields (like an open file handle) that simply can't be meaningfully saved and restored.

Example: Transient Fields

java
import java.io.Serializable;
class Person implements Serializable {
	String name = "Alice";
	transient String password = "secret"; // excluded from serialization
}
public class Main {
	public static void main(String[] args) {
		Person p = new Person();
		System.out.println(p.password);
	}
}

Safe Serialization Practices

Serialization has real security risks if you deserialize untrusted data, since a malicious byte stream can trigger unexpected code execution — only deserialize data from sources you fully trust.

Example: Safe Serialization Practices

java
import java.io.Serializable;
public class Main {
	static class Data implements Serializable {
		int value = 5;
	}
	public static void main(String[] args) {
		System.out.println("Only deserialize data from trusted sources");
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.