next up previous
Next: 1.3.4 Printing Objects Up: 1.3 Java Class Definitions Previous: 1.3.2 Constructors

1.3.3 Defining Instance Methods

The three instance methods getName, getAddress and getPhone in class Entry all simply return the value of a field from the object that received the method call.

Let us define another method for class Entry that does more computation. Assume that we want to define an operation match for the class Entry that takes a string argument keyName and determines whether it matches the name field of the specified Entry object. We could include such a method definition within the definition of class Entry as shown in Figure 2:

class Entry {

  /*fields */
  String name;	
  String address;
  String phone;

  /* constructor */
  Entry(String n, String a, String p) {
    this.name = n;
    this.address = a;
    this.phone = p;
  }	 	 

  /* accessors */
  String getName() { return this.name; }
  String getAddress() { return this.address; }
  String getPhone() { return this.phone; }

  /* other methods */

  /** determines if this matches keyName */
  boolean match(String keyName) {
    return this.name.equals(keyName);
  }	
}
Figure 2: The expanded Entry class

The match method is implemented using the equals method on the String field name. Recall that the String class is built-in to Java. The equals method from the String class takes an argument and returns true if (and only if) it is a String with exactly the same contents as the receiver String object. Hence,

(new Entry("Corky","DH 3104","x 6042")) . match("Corky")
returns
true,
while
(new Entry("Corky","DH 3104","x 6042")) . match("Matthias")
returns
false.

Warning The Java infix operator == can be used to compare objects, but the results of such a comparison are problematic for most applications. On objects, the == operator returns true if (and only if) the both arguments are exactly the same object. Hence, if x is a variable of some object type T, the expression

x == x
returns
true
For distinct object arguments, the == operator returns false. Hence,
new Entry("Corky","DH 3104","x 6042") == new Entry("Corky","DH 3104","x 6042")
returns
false
because each occurrence of new creates a distinct object. For most Java object types including String, the == operator is not a reliable mechanism for testing equality! While the Java compiler follows the convention of creating only one copy of each String constant, it is unwise to rely on this fact because computed strings do not obey this convention. (Neither does the DrJava Interactions pane!)

Finger Exercise

  1. Add the match method to your Entry class. Test your code.
  2. Modify your match method to use the == operator instead of the equals method. Find some test cases where it fails! Hint: A String constant that appears in the DrJava interactions pane will be distinct from any String constant defined in a class in the Definitions pane.

Java Design Rule: There are only two valid uses of the == operator:

The second use is relatively uncommon.


next up previous
Next: 1.3.4 Printing Objects Up: 1.3 Java Class Definitions Previous: 1.3.2 Constructors
Corky Cartwright 2003-07-07