Sitemap
Yasara Fernando

Yasara Fernando

Chapter 14 — Saving Objects

2 min readJul 18, 2022

--

In this chapter, I’m going to discuss serialization and file I/O by referring to the fourteenth chapter of Head First Java by Keith Sierra.

Serializing

Serializing means saving an object. There are a few steps of serializing and the following diagram shows those.

Deserialization

Deserialization is restoring the object back to its original state at some later date in a different JVM. This is the reverse of serialization. There are five steps in deserialization and in the following diagram shows clearly.

The things that happen during deserialization are listed down below.

Get Yasara Fernando’s stories in your inbox

Join Medium for free to get updates from this writer.

· The object is read from the stream

· The JVM determines the object’s class type

· The JVM attempts to find and load the object’s class. If the JVM failed to find/load the class then JVM throws an exception and the process fails.

· A new object is given space on the heap but the serialized object’s constructor does not run. If the constructor runs it would restore the state of the object back to its original ‘new’ state. But we need the object to be restored to the state it had when it was serialized.

· If the object has a non-serialized class somewhere up its inheritance tree the constructor for that non-serializable class will run along with any constructors above that. Whenever the constructor chaining begins it cannot be stopped. That means all superclasses beginning with the first non-serializable one will reinitialize their state.

· The object’s instance variables are given the values from the serialized state. Transient variables are given a value of null for object references and defaults.

The java.io.File class

This class represents a file on a disk but not the content of the file.

Things that can do with a file object are shown in the below diagram.

--

--