Beginning Java Objects: Chapter 3 Objects and Classes
If there comes a time when all references holding onto a particular object have let go of the object's
handle, then as we discussed earlier the object is no longer accessible to the program, like a helium
balloon that has been let loose. Continuing with our previous example:
// We instantiate our first Student object.
Student x = new Student();
// We declare a second reference, but do
// not instantiate a second object.
Student y;
// We pass y a 'handle' on the same object that
// x is holding
// (x continues to hold onto it, too). We now,
// in essence,
// have TWO 'strings' tied to the same 'balloon'.
y = x;
// We now declare a third reference and instantiate
// a second Student object.
Student z = new Student();
// y now lets go of the first Student object
// and grabs onto the second.
y = z;
// Finally, x lets go of the first
// Student object, and grabs onto
// the second, as well; the first
// Student object is now lost to
// the program because we no longer
// have any reference variables
// maintaining a 'handle' on it!
x = z;
As it turns out, if all of an object's handles are lost, it might seem as though the
memory that the object occupies is permanently 'wasted'. In a language like C++, this is
indeed the case, and programmers have to take care to 'recycle' the memory of an object
that is no longer needed before all of its handles are dropped. In Java, on the other
hand, there is a utility called the garbage collector built into the JVM which
automatically recycles 'lost' objects' memory for us. We'll revisit this topic in Chapter
13.
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.
|