Java Reference
In-Depth Information
The class Name can be written to an ObjectOutputStream either directly with
writeObject , or indirectly if it is referenced by an object written to such
a stream. The name and id fields will be written to the stream; the fields
nextID , hashSet , and hash will not be written, nextID because it is static
and the others because they are declared transient . Because hash is a
cached value that can easily be recalculated from name , there is no reas-
on to consume the time and space it takes to write it to the stream.
Default deserialization reads the values written during serialization.
Static fields in the class are left untouchedif the class needs to be loaded
then the normal initialization of the class takes place, giving the static
fields an initial value. Each transient field in the reconstituted object is
set to the default value for its type. When a Name object is deserialized,
the newly created object will have name and id set to the same values as
those of the original object, the static field nextID will remain untouched,
and the transient fields hashSet and hash will have their default values
( false and 0 ). These defaults work because when hashSet is false the
value of hash will be recalculated.
You will occasionally have a class that is generally serializable but has
specific instances that are not serializable. For example, a container
might itself be serializable but contain references to objects that are not
serializable. Any attempt to serialize a non-serializable object will throw
a NotSerializableException .
20.8.3. Serialization and Deserialization Order
Each class is responsible for properly serializing its own statethat is, its
fields. Objects are serialized and deserialized down the type treefrom
the highest-level class that is Serializable to the most specific class. This
order is rarely important when you're serializing, but it can be important
when you're deserializing. Let us consider the following type tree for an
HTTPInput class:
 
Search WWH ::




Custom Search