O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10. Object Serialization

To serialize an object means to write its complete internal state (including its private state) to a byte stream in such a way that a copy of the object can be reconstituted at some later time. When an object is serialized, the entire object graph of all the objects it refers to are serialized along with it. This means it’s possible to serialize complex data structures such as binary trees. It’s also possible to serialize applets and complete GUI component hierarchies. Serialization is a critical piece of Remote Method Invocation (see Chapter 21) because it allows objects (as long as they are Serializable) to be passed across the network. Serialization also provides a convenient way to produce a “deep clone” of an object graph. Serialization can also be used as a trivial technique for saving application state: just serialize the data structures to disk. This requires care, however, because the serialization file format is sensitive to implementation changes in your classes. In Java 1.4, the java.beans package (see Chapter 15) provides a JavaBeans-based persistence mechanism that is often a better alternative for long-term persistence.

This chapter shows how to serialize and deserialize objects, and how to write serializable classes that make simple use of the serialization mechanism. Serialization is actually a surprisingly complex problem, and the serialization mechanism has been updated with each release of Java. This chapter does not attempt ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required