views:

136

answers:

5

What exactly does it mean for a class to be Serializable in Java? Or in general, for that matter...

+2  A: 

It means that instances of the class can be turned into a byte-stream (for example, to be saved to a file) and then converted back into classes again. This reloading could happen in a different instance of the program, or even on a different machine. Serialisation (in any language) involves all sorts of issues, though, especially when you've got references to other objects inside the serialisable one.

David
+2  A: 

Serialization is persisting an object from memory to a sequence of bits, for instance for saving onto the disk. Deserialization is the opposite - reading data from the disk to hydrate/create an object.

In the context of your question, it is an interface that if implements in a class, means that it can be automatically be serialized and deserialized by the different serializers.

Oded
Also note that all fields not explicitly marked otherwise will be serialized too. This means that you can save a complex datastructure easily just by serializing the root object.
Thorbjørn Ravn Andersen
There is nothing 'textual' about what the Serializable interface buys you.
EJP
@EJP - true enough. Removed "textual" reference.
Oded
A: 

Serializable is called in like an interface but its more like a flag to the compiler. It says this object can be saved. All the Objects instance variables with the exception of none serializable objects and ones mark volatile will be saved.

Imagine your application can change colour as an option, without keeping that setting external you would need to change the colour every time you ran it.

AphexMunky
It is not a 'flag to the compiler'. It is a flag to the Serialization subsystem, at runtime.
EJP
@EJP - Thanks, didn't know that
AphexMunky
With respect, why write it when you don't know it to be true? You've also left out 'transient'. All in all a poor answer, sorry.
EJP
If I hadn't of wrote it I wouldn't of been corrected and would be worse off. All the other answers have left off transient too. You didn't even write an answer, you're just trolling other peoples.
AphexMunky
+1  A: 

Serialization involves saving the current state of an object to a stream, and restoring an equivalent object from that stream. The stream functions as a container for the object

org.life.java
A: 

Just to add to the other answers and with regards to generality. Serialization is sometimes known as archiving, for example in Objective-C.

Greg Sexton