Externalizable
Stay organized with collections
Save and categorize content based on your preferences.
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. These methods must explicitly
coordinate with the supertype to save its state. These methods supersede
customized implementations of writeObject and readObject methods.
Object Serialization uses the Serializable and Externalizable
interfaces. Object persistence mechanisms can use them as well. Each
object to be stored is tested for the Externalizable interface. If
the object supports Externalizable, the writeExternal method is called. If the
object does not support Externalizable and does implement
Serializable, the object is saved using
ObjectOutputStream.
When an Externalizable object is
reconstructed, an instance is created using the public no-arg
constructor, then the readExternal method called. Serializable
objects are restored by reading them from an ObjectInputStream.
An Externalizable instance can designate a substitution object via
the writeReplace and readResolve methods documented in the Serializable
interface.
Public Method Summary
abstract
void
|
readExternal( ObjectInput in)
The object implements the readExternal method to restore its
contents by calling the methods of DataInput for primitive
types and readObject for objects, strings and arrays.
|
abstract
void
|
writeExternal( ObjectOutput out)
The object implements the writeExternal method to save its contents
by calling the methods of DataOutput for its primitive values or
calling the writeObject method of ObjectOutput for objects, strings,
and arrays.
|
Public Methods
The object implements the readExternal method to restore its
contents by calling the methods of DataInput for primitive
types and readObject for objects, strings and arrays. The
readExternal method must read the values in the same sequence
and with the same types as were written by writeExternal.
Parameters
in |
the stream to read data from in order to restore the object |
public
abstract
void
writeExternal
(ObjectOutput out)
The object implements the writeExternal method to save its contents
by calling the methods of DataOutput for its primitive values or
calling the writeObject method of ObjectOutput for objects, strings,
and arrays.
Parameters
out |
the stream to write the object to |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eExternalizable\u003c/code\u003e interface provides complete control over the serialization and deserialization process, requiring explicit handling of object state by implementing \u003ccode\u003ewriteExternal\u003c/code\u003e and \u003ccode\u003ereadExternal\u003c/code\u003e methods.\u003c/p\u003e\n"],["\u003cp\u003eUnlike \u003ccode\u003eSerializable\u003c/code\u003e, \u003ccode\u003eExternalizable\u003c/code\u003e does not automatically save and restore object fields; the class is fully responsible for this using \u003ccode\u003eObjectInput\u003c/code\u003e and \u003ccode\u003eObjectOutput\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eWhen an object implementing \u003ccode\u003eExternalizable\u003c/code\u003e is serialized, only its class identity is written; the \u003ccode\u003ewriteExternal\u003c/code\u003e method is then called to save the object's contents.\u003c/p\u003e\n"],["\u003cp\u003eDuring deserialization, an instance is created using the no-arg constructor, and the \u003ccode\u003ereadExternal\u003c/code\u003e method is invoked to populate the object's data from the stream.\u003c/p\u003e\n"],["\u003cp\u003eExternalizable objects can use \u003ccode\u003ewriteReplace\u003c/code\u003e and \u003ccode\u003ereadResolve\u003c/code\u003e to designate substitution objects, offering flexibility in serialization behavior.\u003c/p\u003e\n"]]],["Externalizable objects control their serialization process. During serialization, only the class identity is written; the object's data is handled by the `writeExternal` method, using `DataOutput` for primitives and `writeObject` for objects. Deserialization involves creating an instance via the no-arg constructor, followed by a call to `readExternal` which uses `DataInput` and `readObject`. Classes implementing `Externalizable` must coordinate with supertypes and use specific sequences to read/write data.\n"],null,["# Externalizable\n\npublic interface **Externalizable** implements [Serializable](../../../reference/java/io/Serializable.html) \nOnly the identity of the class of an Externalizable instance is\nwritten in the serialization stream and it is the responsibility\nof the class to save and restore the contents of its instances.\n\nThe writeExternal and readExternal methods of the Externalizable\ninterface are implemented by a class to give the class complete\ncontrol over the format and contents of the stream for an object\nand its supertypes. These methods must explicitly\ncoordinate with the supertype to save its state. These methods supersede\ncustomized implementations of writeObject and readObject methods. \n\n\nObject Serialization uses the Serializable and Externalizable\ninterfaces. Object persistence mechanisms can use them as well. Each\nobject to be stored is tested for the Externalizable interface. If\nthe object supports Externalizable, the writeExternal method is called. If the\nobject does not support Externalizable and does implement\nSerializable, the object is saved using\nObjectOutputStream. \nWhen an Externalizable object is\nreconstructed, an instance is created using the public no-arg\nconstructor, then the readExternal method called. Serializable\nobjects are restored by reading them from an ObjectInputStream. \n\n\nAn Externalizable instance can designate a substitution object via\nthe writeReplace and readResolve methods documented in the Serializable\ninterface. \n\n##### See Also\n\n- [ObjectOutputStream](../../../reference/java/io/ObjectOutputStream.html)\n- [ObjectInputStream](../../../reference/java/io/ObjectInputStream.html)\n- [ObjectOutput](../../../reference/java/io/ObjectOutput.html)\n- [ObjectInput](../../../reference/java/io/ObjectInput.html)\n- [Serializable](../../../reference/java/io/Serializable.html) \n\n### Public Method Summary\n\n|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [readExternal](../../../reference/java/io/Externalizable.html#readExternal(java.io.ObjectInput))([ObjectInput](../../../reference/java/io/ObjectInput.html) in) The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays. |\n| abstract void | [writeExternal](../../../reference/java/io/Externalizable.html#writeExternal(java.io.ObjectOutput))([ObjectOutput](../../../reference/java/io/ObjectOutput.html) out) The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays. |\n\nPublic Methods\n--------------\n\n#### public abstract void\n**readExternal**\n([ObjectInput](../../../reference/java/io/ObjectInput.html) in)\n\nThe object implements the readExternal method to restore its\ncontents by calling the methods of DataInput for primitive\ntypes and readObject for objects, strings and arrays. The\nreadExternal method must read the values in the same sequence\nand with the same types as were written by writeExternal. \n\n##### Parameters\n\n| in | the stream to read data from in order to restore the object |\n|----|-------------------------------------------------------------|\n\n##### Throws\n\n| [IOException](../../../reference/java/io/IOException.html) | if I/O errors occur |\n| [ClassNotFoundException](../../../reference/java/lang/ClassNotFoundException.html) | If the class for an object being restored cannot be found. |\n|------------------------------------------------------------------------------------|------------------------------------------------------------|\n\n#### public abstract void\n**writeExternal**\n([ObjectOutput](../../../reference/java/io/ObjectOutput.html) out)\n\nThe object implements the writeExternal method to save its contents\nby calling the methods of DataOutput for its primitive values or\ncalling the writeObject method of ObjectOutput for objects, strings,\nand arrays. \n\n##### Parameters\n\n| out | the stream to write the object to |\n|-----|-----------------------------------|\n\n##### Throws\n\n| [IOException](../../../reference/java/io/IOException.html) | Includes any I/O exceptions that may occur |\n|------------------------------------------------------------|--------------------------------------------|"]]