Serialization in general means that at the language level there exists a certain level at which entities of the language can be "persisted" in some form, and read back from that persisted form.
That persisted form is more often than not a stream of bytes, which is saved into a file; and you read back from that file in order to recreate the entity at runtime.
In Java, the basic entity which can be persisted/serialized is an Object
; and in order for it to be a candidate for serialization, it must implement the Serializable
interface.
Now, this particular interface is a trap; it has many, many conditons to it being implemented correctly. Among others:
- ideally, you want all of your
Serializable
instances to define a private static final long serialVersionUUID
, and yes, here, the name of the variable matters, it cannot be anything else;
- you may have instance variables in your object which you do not want to serialize: those should be marked as
transient
;
- you should ensure that all instance variables, apart from the
transient
ones, are themselves serializable; if not you must implement, at the very least, the {read,write}Object()
methods, or to an even finer grained resolution, readResolve()
and writeReplace()
.
In other words: good luck!
Note: maybe you also want to have a look at Externalizable
.