Beginning Java Objects: Chapter 3 Objects and Classes
Memory allocation using handles might look something like this conceptually:
With this approach, each object is allocated in memory only once; the Student object knows how to
find and communicate with its advisor (Professor) object whenever it needs to through its handle,
and vice versa.
What do we gain by defining the Student's advisor attribute as a reference to a Professor
object, instead of merely storing the name of the advisor as a String attribute of the Student object?
- For one thing, we can ask the
Professor object its name
whenever we need it by invoking its getName method. Why is
this important? To avoid data redundancy and the potential for loss of
data integrity. If the Professor object's name changes for
some reason, the name will only be stored in one place: encapsulated as an
attribute within the Professor object that 'owns' the name,
which is precisely where it belongs. If we instead were to store the
Professor's name both as a String attribute of
the Professor object and as a String attribute of
the Student object, we'd have to remember to update the name
in two places any time the name changed (or three, or four, or however many
places this Professor's name is referenced as an advisor of
countless Students). If we were to forget to do so, then the
name of the Professor would be 'out of synch' from one
instance to another.
- Just as importantly, by maintaining a handle on the
Professor object via the advisor attribute of
Student, the Student object can also request
other services of this Professor object via whatever methods
are defined for the Professor class. A Student
object may, for example, ask its advisor (Professor) object
where the Professor's office is located, or what classes the
Professor is teaching so that the Student can
sign up for one of them.
- Another advantage of using object handles from an implementation
standpoint is that they also reduce memory overhead. Storing a handle to an
object only requires 32 or 64 bits of memory, depending on the programming
language, instead of however many bytes of storage the referenced object as
a whole occupies in memory. If we were to have to make a copy of an entire
object every place we needed to refer to it in our application, we'd
quickly exhaust available memory.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|