Possible Duplicate:
What is the difference between Serializable and Externalizable in Java?
what is the difference between serialization and externalization in java?
Possible Duplicate:
What is the difference between Serializable and Externalizable in Java?
what is the difference between serialization and externalization in java?
Basically, the difference between Serializable
and Externalizable
is that with classes which implement Serializable
, the serialization of the object is taken care of automatically, while classes that implement Externalizable
is responsible for serializing itself, without the help of default serialization procedures.
There is more information provided in the API Specification for the Externalizable
interface, and the Serializable
interface. From the Externalizable
interface documentation:
Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes.
The Discover the secrets of the Java Serialization API article has a discussion on the Externalizable
interface in the "Create Your Own Protocol: the Externalizable Interface" section.
I recommend reading an article called Understand When to Serialize v. Externalize Objects in Java that described the differences between serialization and externalization.
First is describes what serialization is:
The serialization of objects in Java allows you to make a byte sequence from any object that has implemented the Serializable interface; it also allows you to turn that byte sequence back into an object.
Next it describes a situation in which externalization might be preferable to serialization:
There might be times when you have special requirements for the serialization of an object. For example, you may have some security-sensitive parts of the object, like passwords, which you do not want to keep and transfer somewhere. Or, it may be worthless to save a particular object referenced from the main object because its value will become worthless after restoring.
You can control the process of serialization by implementing the Externalizable interface instead of Serializable. This interface extends the original Serializable interface and adds writeExternal() and readExternal(). These two methods will automatically be called in your object's serialization and deserialization, allowing you to control the whole process.
I recommend reading the entire article, because the excerpts above do not cover the details. The article also contains several code snippets you might find useful.