A. Serialization
Serialization is the process of taking the in-memory representation of an object and storing it (persisting) to a file so that it survives even after the program which created it exits. In comparison to other languages, Java provides straightforward support for serialization. However, there are several steps necessary for successful serialization.
First, your object must implement the
java.io.Serializable interface. This interface simply marks
your object as semantically able to be serialized. Not every type of Java
object can be serialized. Its easy to imagine how a String
can be written to and recovered from a file. After all, its just a list of
characters, that's basically what a file is anyway. However, its much more
difficult to imagine how an object like a Thread could be
serialized. A Thread is much more than just data, its a
running programming with its own context. As you might imagine, in Java
Strings implement the Serializable interface,
Threads do not.
Second, all of the instance variables in your object must be
serializable. Fortunately, most of the data objects (Strings,
Vectors, etc) in the main Java library are serializable. As I
mentioned before, some, like Thread are not. When in
doubt, look at the documentation. But what if your class needs to have
some non-serializable object as an instance variable? This is unlikely in
Darkstar, but perfectly possible. In that case, you need to mark that
object as transient. For example:
protected transient Thread ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access