• Tidak ada hasil yang ditemukan

Working with Objects

Dalam dokumen Pelajari tentang Advanced topics in Java (Halaman 48-52)

Introduction to Objects

2.8 Working with Objects

The following will be printed:

Name of part: Air Filter Price: $8.75

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: Ball Joint price: 29.95 name: Air Filter

price: 8.75

5768 b 3472

a

Figure 2-5. After creation of two Part objects

name: Air Filter price: 9.50

3472 a

3472 c Figure 2-6. After assigning a to c

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.

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.

2.8.2 Losing Access to an Object

Consider the following:

Part a = new Part("Air Filter", 8.75);

Part b = new Part("Ball Joint", 29.95);

Assume these statements create the situation shown in Figure 2-7.

name: Ball Joint price: 29.95 name: Air Filter

price: 8.75

5768 b 3472

a

Figure 2-7. After creation of two Part objects

name:Ball Joint price:29.95 name:Air Filter

price:8.75

5768 b 5768

a

Figure 2-8. After assigning b to a Suppose we execute this statement:

a = b;

The situation changes to that shown in Figure 2-8.

Both a and b now have the same value, 5768. They both point to the Ball Joint object. In effect, when we change the value of a, we lose access to the Air Filter object. When no variable points to an object, the object is inaccessible and cannot be used. The storage occupied by the object will be garbage collected by the system and returned to the pool of available storage. This takes place automatically without any action on the part of the program.

However, suppose we had written this:

c = a; // c holds 3472, address of "Air Filter"

a = b; // a, b hold 5768, address of "Ball Joint"

Now, we would still have access to Air Filter via c.

2.8.3 Comparing Object Variables

Consider the following that creates two identical, but separate, objects and stores their addresses in a and b:

Part a = new Part("Air Filter", 8.75);

Part b = new Part("Air Filter", 8.75);

Assume these statements create the situation shown in Figure 2-9.

name:Air Filter price:8.75 name:Air Filter

price: 8.75

4000 b 2000

a

Figure 2-9. After creation of two identical objects

Since the objects are identical, it may come as a surprise that the following condition is false:

a == b

However, if you remember that a and b contain addresses and not objects, then we are comparing the address in a (2000) with the address in b (4000). Since these are different, the comparison is false.

Two object variables would compare equal only when they contain the same address (in which case they point to the same object). This could happen, for instance, when we assign one object variable to another.

We would, of course, need to know whether two objects are the same. That is, if a and b point to two objects, are the contents of these objects the same? To do this, we must write our own method that compares the fields, one by one.

Using the class Part as an example, we write a method equals, which returns true if one object is identical to another and false otherwise. The method can be used as follows to compare Part objects pointed to by a and b:

if (a.equals(b)) ...

The method simply checks whether the name fields and the price fields of the two objects are the same. Since the name fields are String objects, we call the equals method of the String class to compare them.1

public boolean equals(Part p) {

return name.equals(p.name) && (price == p.price);

}

In the method, the variables name and price (without being qualified) refer to the fields of the object via which the method is invoked. Suppose we had used the following expression:

a.equals(b)

1There฀is฀no฀conflict฀in฀using฀the฀same฀name฀equals฀to฀compare฀Parts฀and฀Strings.฀If฀equals฀is฀invoked฀via฀a฀Part฀object,฀then฀

the฀equals฀method฀from฀the฀Part฀class฀is฀used.฀If฀equals฀is฀invoked฀via฀a฀String฀object,฀then฀the฀equals฀method฀from฀the฀String฀

class฀is฀used.

The variables refer to the fields a.name and a.price. Of course, p.name and p.price refer to the fields of the argument to equals (b, in the example). In effect, the return statement becomes this:

return a.name.equals(b.name) && (a.price == b.price);

Now, suppose we have these statements:

Part a = new Part("Air Filter", 8.75);

Part b = new Part("Air Filter", 8.75);

(a == b) is false (since a and b hold different addresses), but a.equals(b) is true (since the contents of the objects they point to are the same).

Dalam dokumen Pelajari tentang Advanced topics in Java (Halaman 48-52)