4.2. Serialization Basics

Not all Java classes are serializable. To indicate that you want a class to be serializable, you must implement the java.io.Serializable interface. This interface has no methods; rather, it simply acts as a marker interface indicating a class's willingness to have its instances serialized. In Listing 4-1, the Person class is serializable, but the Humanoid class is not. When an instance of Person is written to a stream, the serialization architecture uses reflection to extract the field values and write them to the stream.

Listing 4-1. The Humanoid and Person Classes
 public abstract class Humanoid { protected int noOfHeads; private static int totalHeads; public Humanoid() { this(1); } public Humanoid(int noOfHeads) { ...

Get Component Development for the Java™ Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.