23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong> I/O<br />

• All changes to transient fields—changing their type, their names, adding or removing<br />

them, etc. Serialization ignores all transient fields.<br />

• Adding an instance field. When an instance of the older version of the class is<br />

deserialized, the new field will merely be set to its default value (0 for numeric types,<br />

false for booleans, null for object types) when an object in that class is deserialized.<br />

• Adding or removing an interface (except the Serializable interface) from a class.<br />

Interfaces say nothing about the instance fields of a class.<br />

• Adding or removing inner classes, provided no nontransient instance field has the type<br />

of the inner class.<br />

• Changing the access specifiers of a field. Serialization does not respect access<br />

protection.<br />

• Changing a field from static to nonstatic or transient to nontransient. This is the same<br />

as adding a field.<br />

The following incompatible changes prevent deserialization of serialized objects:<br />

• Changing the name of a class.<br />

• Changing the type of an instance field.<br />

• Changing the name of an instance field. This is the same as removing the field with<br />

the old name.<br />

• Changing a field from nonstatic to static or nontransient to transient. This is the same<br />

as removing the field.<br />

• Changing the superclass of a class. This may affect the inherited state of an object.<br />

• Changing the writeObject() or readObject() method (discussed later) in an<br />

incompatible fashion.<br />

• Changing a class from Serializable to Externalizable (discussed later) or<br />

Externalizable to Serializable.<br />

11.7.2 Version IDs<br />

To help identify compatible or incompatible classes, each class may have a stream unique<br />

identifier, SUID for short. This is a long calculated by a special hash function and stored in<br />

the class in a static field called serialVersionUID like this:<br />

public class UnicodeApplet extends Applet {<br />

static final long serialVersionUID = 5913267123532863320L;<br />

// ...<br />

Every time you release a new version of a class that makes an incompatible change, you<br />

should change the serialVersionUID field. The serialver tool, included with the JDK,<br />

calculates the appropriate hash of the class's name and fields. For example:<br />

% serialver UnicodeApplet<br />

UnicodeApplet: static final long serialVersionUID =<br />

5913267123532863320L;<br />

There's also a GUI interface available with the -show flag, as shown in Figure 11.2.<br />

250

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!