THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

1.8. Methods and Parameters


Objects of the previously defined Point class are exposed to manipulation by any code that has a reference
to a Point object, because its fields are declared public. The Point class is an example of the simplest
kind of class. Indeed, some classes are this simple. They are designed to fit purely internal needs for a package
(a group of cooperating classes) or to provide simple data containers when those are all you need.


The real benefits of object orientation, however, come from hiding the implementation of a class behind
operations performed on its data. Operations of a class are declared via its methodsinstructions that operate on
an object's data to obtain results. Methods access internal implementation details that are otherwise hidden
from other objects. Hiding data behind methods so that it is inaccessible to other objects is the fundamental
basis of data encapsulation.


If we enhance the Point class with a simple clear method (to clear, or reset the coordinates to zero), it
might look like this:


public void clear() {
x = 0.0;
y = 0.0;
}


The clear method has no parameters, hence the empty () pair after its name; clear is declared void
because it does not return any value. Inside a method, fields and other methods of the class can be named
directlywe can simply say x and y without an explicit object reference.


1.8.1. Invoking a Method


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. Well-designed classes usually hide their data so that it
can be changed only by methods of that class.


To invoke a method, you provide an object reference to the target object and the method name, separated by a
dot. Arguments are passed to the method as a comma-separated list of values enclosed in parentheses.
Methods that take no arguments still require the parentheses, with nothing between them.


A method can return only a single value as a result. To return more than one value from a method, you must
create an object whose purpose is to hold return values in a single unit and then return that object.


When a method is invoked, the flow of execution leaves the current method and starts executing the body of
the invoked method. When the invoked method has completed, the current method continues execution with
the code after the method invocation. When we start executing the body of the method, the object that was the
target of the method invocation is now the current or receiver object, from the perspective of that method. The
arguments passed to the method are accessed via the parameters the method declared.


Here is a method called distance that's 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 = x - that.x;
double ydiff = y - that.y;
return Math.sqrt(xdiff xdiff + ydiff ydiff);
}

Free download pdf