Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 1

Learn how to declare classes and create objects

  • Print
  • Feedback

Page 3 of 4

Why doesn't obj1 or obj2 prefix the i variable in printHello()? If you're curious, check out next month's article.

Now that you have some insight into what an object is and how it is referenced, you might be curious about its internal structure. Figure 2 sheds some light on that structure.

Figure 2. Inside a CODemo2 object

Figure 2 shows two reference variables -- obj1 and obj2 -- that reference two CODemo2 objects. Actually, each variable contains the address of an object descriptor that contains information about the object. Among other things, the descriptor holds the addresses to one block of memory that contains the object's fields, and one that contains the byte code of each CODemo2 method.

A close look at Figure 2 gives the impression that Java supports pointers -- variables that contain the addresses of other variables. At the language level, Java does not support pointers. Instead, Java supports references -- abstract object identifiers. The underlying JVM determines how to implement a reference. For example, one kind of JVM might assign a number to a reference variable that indexes into a table of pointers to objects, whereas another kind of JVM might assign the object's memory address to the reference variable. (In this case, the reference variable is basically a pointer to the object.) Because you don't know (and don't need to know) what assigns to a reference variable, just think of a reference variable as being able to reference an object in some fashion -- and forget about pointers!

Destroying objects

C++ requires all heap-created objects to be explicitly destroyed -- by using C++'s delete keyword. Java, however, assumes responsibility for destroying created objects. The JVM's garbage collector, which runs at periodic intervals, examines each object to see if it is referenced by at least one variable. If not, the garbage collector destroys the object, freeing memory in the process.

Internally, the JVM maintains a reference count for each object. When the creation operator first creates an object (as in new CODemo2 ();), and the assignment operator assigns that reference to an object reference variable (as in CODemo2 obj1 = new CODemo2 ();), the reference count initializes to one (behind the scenes). Whenever that reference assigns to another variable of the same reference data type, the reference count increments by one. However, if the null literal assigns to the variable, the reference count decrements by one. As soon as the reference count reaches zero -- and the next time the garbage collector runs -- the JVM destroys the object. Consider the following example:

// Create an object and assign its reference to myObjectReference.
CODemo2 myObjectReference = new CODemo2 ();           // Internal reference count set to 1.
// Assign myObjectReference's reference value to anotherObjectReference.
CODemo2 anotherObjectReference = myObjectReference;   // Internal reference count set to 2.
// Destroy one of the references.
myObjectReference = null;                             // Internal reference count set to 1.
// Destroy the other reference.
anotherObjectReference = null;                        // Internal reference count set to 0.  
// At this point, the object will be destroyed when the garbage collector runs.


Review

This article introduced OOP methodology and showed how to declare classes and create objects from those classes. I also presented the relationship between classes and Java programs (notably applications), as well as object destruction.

  • Print
  • Feedback

Resources