Java Reference
In-Depth Information
Suppose the Air Filter object is stored at location
3472
and the Ball Joint object is stored at location
5768
.
Then the value of
a
would be
3472
, and the value of
b
would be
5768
. After the two objects have been created, we have
the situation shown in Figure
2-5
.
name: Air Filter
price: 8.75
name: Ball Joint
price: 29.95
3472
a
5768
b
Figure 2-5.
After creation of two
Part
objects
Suppose we then assign
a
to
c
, like this:
Part c = a; // assign 3472 to c
This assigns the value
3472
to
c
; in effect,
c
(as well as
a
) now points to the Air Filter object. We can use either
variable to access the object. For instance, the following sets the price of the Air Filter object to
9.50
:
c.setPrice(9.50);
We have the situation shown in Figure
2-6
.
name: Air Filter
price: 9.50
3472
a
3472
c
Figure 2-6.
After assigning
a
to
c
If we now retrieve the price of object
a
with the following, the (new) price of Air Filter would be returned:
a.getPrice(); // returns the price 9.50
Suppose we write this statement:
c = b; // assign 5768 to c
c
is assigned
5768
and now points to the Ball Joint object. It no longer points to Air Filter. We can use
b
or
c
to access Ball Joint data. If we have the
address
of an object, we have all the information we need to manipulate
the object.
Search WWH ::
Custom Search