Java Reference
In-Depth Information
licly 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 paren-
theses. 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 in-
vocation 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);
}
The return statement causes a method to stop executing its body and
return execution to the invoking method. If an expression is part of the
return statement then the value of that expression is returned as the
value of the method invocation. The type of the expression must be
 
Search WWH ::




Custom Search