Objects and Their Data


You have already learned that objects, like arrays, contain clusters of data. You have also learned that the data in an object, unlike the data in an array, can be of differing types. The number, types, and names of an object's data elements are all defined in the object's class definition.

Let's look at a very simple example. Here is a very simple class definition:

public class Person {   int    age;   short  weight; } 

This is our first example of a class that does not contain a method called main. In fact, this class definition has no methods at all. The class just defines a bundle of data. The bundle contains an int called age and a short called weight.

To create an individual instance of this class, you would use the following code:

Person keara; keara = new Person();

The first line is a declaration. Like all other declarations, it tells the compiler that you will be using a variable called keara and it will be of a certain type. At first glance, it appears that the type of keara will be Person; this is almost true, but not quite. Actually, the declaration says keara will be a reference to an object, and that object will be an instance of the Person class. If the distinction seems subtle, it is also very important.

The situation is similar to what we saw in the previous chapter, in the context of arrays. The declaration int[] temperatures; says that temperatures will be a reference (in accessible memory) to an array (in inaccessible memory). Similarly, the declaration Person keara; says that keara will be a reference (in accessible memory) to an array (in inaccessible memory). In both cases, the declaration does not cause construction of the array or object. Nothing gets constructed until new is executed.

When the second line (keara = new Person();) executes, an object is constructed, using the class definition as a kind of stencil or cookie cutter. The JVM knows which class definition to use (remember, a Java application can consist of many class files) because the class name appears after new and before the empty parentheses. As with arrays, the invocation of new constructs an object in inaccessible memory and returns a reference to that object. The reference is stored in the variable keara, so keara now refers to the newly created object. At this point, the situation is as shown in Figure 7.2.

click to expand
Figure 7.2: Reference and object

The figure shows that the object contains its own set of data variables, with names and types as specified in the class definition source file. When an object is constructed, its data variables (called fields) are initialized in the same way array components are initialized. Numeric fields are initialized to 0, char fields are initialized to the null character, and boolean fields are initialized to false.

Now you can use the reference keara to manipulate the object's fields. The following code writes and reads the fields of an object, using a new notation:

keara.age = 8; short f = keara.weight;

In both lines, you use the following syntax to refer to an object's field:

 Object_reference.field_name 

The period is pronounced "dot." So if you were reading the first code line out loud, you would say, "Keara dot age equals eight."

The DataLab animated illustration shows the construction of an object and the use of a reference to access fields of that object. Please take a moment now to run the animation by typing java objects.DataLab. Figure 7.3 shows DataLab's initial display.

click to expand
Figure 7.3: DataLab

Press the "Run" button to view the animation.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net