next up previous
Next: The Union and Composite Up: Java Class Definitions Previous: Constructors

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 {
  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 for identity. In many contexts, the results of such a comparison are problematic. 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! For example, Java does not guarantee that it creates only one copy of a String constant.


Finger Exercise 1.5.3.1

  1. Add the match method to the Entry class in the file Entry.java. 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: The Union and Composite Up: Java Class Definitions Previous: Constructors
Corky Cartwright 2004-02-05