Previous | Table of Contents | Next |
Objects in general do not operate directly on the data of other objects although, as you saw in the Point class, a class can make its fields publicly accessible. In general, though, well-designed classes hide their data so that it can be changed only by methods of that class. To invoke a method, you provide an object reference and the method name, separated by a dot (.). Parameters are passed to the method as a comma-separated list of values enclosed in parentheses. Methods that take no parameters still require the parentheses, with nothing between them. The object on which the method is invoked (the object receiving the method invocation) is often known as the receiving object of the receiver.
A method may return a single value as a result. To return more than one value from a method, create an object whose sole purpose is to hold return values in a single unit, and return that object.
Here is a method called distance thats part of the Point class shown in previous examples. The distance method accepts another Point object as a parameter, computes the Euclidean distance between itself and the other point, and returns a double-precision floating-point result:
public double distance(Point that) { double xdiff, ydiff; xdiff = x - that.x; ydiff = y - that.y; return Math.sqrt(xdiff ( xdiff + ydiff * ydiff); }
Based on our lowerLeft and upperRight objects created in the section on instantiating objects previously, you could invoke distance like this:
double d = lowerLeft.distance(upperRight);
After this statement executes, the variable d contains the Euclidean distance between lowerLeft and upperRight.
Occasionally, the receiving object needs to know its own reference. For example, the receiving object might want to add itself to a list of objects somewhere. An implicit reference named this is available to methods, and this is a reference to the current (receiving) object. The following definition of clear is equivalent to the one just presented:
public void clear() { this.x = 0; this.y = - }
You usually use this as a parameter to other methods that need an object reference. The this reference also can be used to explicitly name the members of the current object. Heres another one of Points methods, named move, that sets the x and y fields to specified values:
public void move(double x, double y) { this.x = x; this.y = y; }
This move method uses this to clarify which x and y are being referred to. Naming the parameters of move x and y is reasonable, because you pass x and y coordinates to the method. But then those parameters have the same names as Points fields, and therefore the parameter names are said to hide the field names. If we simply wrote x=x we would assign the value x parameter to itself, not to the x field as required. The expression this.x refers to the objects x field, not the x parameter of move.
Just as you can have per-class static fields, you also can have per-class static methods, often known as class methods. Class methods are usually intended to do class-like operations specific to the class itself, and usually on static fields, not on specific instances of that class. Class methods are declared using the static keyword, and therefore are also known as static methods.
As with the term field, when you see method, it generally means a per-object method, although the term non-static method is sometimes used for clarity.
Why would you need static methods? Consider the Sony Walkman factory again. The record of the next serial number to be assigned is held in the factory, not in every Walkman. A method to operate on the factorys copy of the next available serial number must be a static method, not a method to operate on specific Walkman objects.
The implementation of distance in the previous example uses the static method Math.sqrt to calculate a square root. The Math class supports many methods useful for general mathematical manipulation. These methods are declared as static methods because they do not act on any particular object, but group a related set of functionality in the class itself.
A static method cannot directly access non-static members. When a static method is invoked, theres no specific object reference for the method to operate on. You could work around this by passing an explicit object reference as a parameter to the static method. In general, however, static methods do class kinds of operations and non-static methods do object kinds of things. Asking a static method to work on object fields is like asking the Walkman factory to change the serial number of a Walkman hanging on the belt of a jogger in Golden Gate Park.
Previous | Table of Contents | Next |