Java Reference
In-Depth Information
Using
Externalizable
results in code that is more difficult to maintain. For instance, if we
were to add a new class member to the
DVD
class—say, a serializable type such as boolean—
our preceding read and write code would not change. However, if
DVD
implemented
Externalizable
instead of
Serializable
, then we would have to update the
readExternal
and
writeExternal
methods to incorporate the change. In addition, the externalization methods
should include logic to check for specific versions of serial version IDs to determine if they
were dealing with older versions of the class. In a production system, this can be an annoy-
ance if you have a lot of classes that tend to change over time. Table 6-1 presents a comparison
of serialization to externalization.
■
Tip
The use of
transient
in a class that implements
Externalizable
is not required. Since the
Externalizable
interface requires that the details of reading and writing the object's state be defined,
the
transient
keyword is not necessary. In fact, it is completely ignored in an
Externalizable
object.
■
Note
Since application performance is not a consideration in our design; we have opted for plain seriali-
zation over externalization in our implementation of Denny's DVDs version 2.0.
Table 6-1.
Comparing Serialization and Externalization
Serialization
Externalization
Advantage Goes To . . .
Serializable
Easier to use. Just implement
More complex. Must implement
Serializable
and use
the
Externalizable
interface
writeObject
and
readObject
methods
readExternal
and
writeExternal
.
to persist state.
Externalizable
Less efficient algorithm. Uses
Better performance. No need to
reflection to determine object's
use reflection since you write
makeup.
the code.
Externalizable
More data gets serialized due to
More control over what gets
larger class description and
serialized.
versioning information.
Introducing RMI
An important objective of the SCJD exam is for the examinee to develop a solution that will
allow machines on a network to exchange messages. Such communication, known as distrib-
uted computing, can be a challenging task, but RMI is one of the helpful tools at your disposal.
However, RMI is not the only game in town. There are other technologies, such as RPC, Com-
mon Object Request Broker Architecture (CORBA), and Microsoft's .NET technology. In fact,
RMI can be thought of as object-oriented RPC.