What is a serializable object?

Serializing in general means to save an objects state into a 'saveable' format (like saving to disk) so that it can be deserialized later on into an actual object. It is usually done to also send an object over the network in case of remote calls. If you dont want save and also if you dont want to send an object over the wire you can ignore the serializable part (in Java you dont implement the Serializable interface)


Normally objects are random access, that is, you can specify any part of an object (property or field) and access that part directly. That's all well and fine if you're using RAM to store an object, because RAM is Random Acess Memory and is therefore suited to the job.

When you need to store your object on a medium that is not traditionally random access, for instance disk, or you need to transfer an object over a stream medium (such as the network) then the object needs to be converted into a form that is suitable to the relevant medium. This conversion process is called serialization, because the structured object is flattened or serialized, making it more amenable to being stored for the long term, or transferred over the network.

Why not just copy the bits comprising the object in RAM to disk, or send it as an opaque blob over the network? ... you may ask. A few issues:

  1. Often the format that the object is stored in memory is proprietary and therefore not suitable for public consumption--the way in which it is stored in memory is optimised for in-memory use.
  2. When an object references other objects, those references only have meaning within the context of the running application. It would not be possible to deserialize the object meaningfully unless during the serialization process, the object graph was walked and serialized accordingly. There may be a need to translate those references into a form that has meaning outside the context of an application instance.
  3. There may be an interoperability requirement between heterogeneous systems, in which case a standard means of representing the object is required (typically some form of XML is chosen for this).

Object serialization is storing the instance's state so you can reconstruct that instance again later.

In most (C# and Java), a serializable object is "marked". In Java you need to implement Serializable. In C# you need to use [Serializable].

Once the object is serialized you can store it in a file or send it over the network.

Think of it like going through every instance variable of an instance and storing its value, separated by some separator (although, it's a lot more sophisticated than that; think of what happens if you have instance variables of non-primitive types, you're gonna have to store all the values inside those, too).

One use of it would be saving a game.


An object that can be converted to bits and stored on a medium, such as a hard drive. http://en.wikipedia.org/wiki/Serialization