Java Reference
In-Depth Information
Suppose that the heading of the method
pow
is:
public static double
pow(
double
base,
double
exponent)
In this heading, you can see that the formal parameters of
pow
are
base
and
exponent
.
Consider the following statements:
double
u = 2.5;
double
v = 3.0;
double
x, y, w;
x = pow(u, v);
//Line 1
y = pow(2.0, 3.2);
//Line 2
w = pow(u, 7);
//Line 3
In Line 1, the method
pow
is called with the parameters
u
and
v
. In this case, the values of
u
and
v
are passed to the method
pow
. In fact, the value of
u
is copied into
base
and the
value of
v
is copied into
exponent
. The variables
u
and
v
that appear in the call to the
method
pow
in Line 1 are called actual parameters of that call. In Line 2, the method
pow
is called with the parameters
2.0
and
3.2
. In this call, the value
2.0
is copied into
base
and
3.2
is copied into
exponent
. In this call to the method
pow
, the actual
parameters are
2.0
and
3.2
, respectively. Similarly, in Line 3, the actual parameters of
the method
pow
are
u
and
7
. The value of
u
is copied into
base
, and
7.0
is copied into
exponent
.
We now present the following two definitions:
Formal parameter: A variable declared in the method heading.
Actual parameter: A variable or expression listed in a call to a method.
7
SYNTAX: VALUE-RETURNING METHOD
The syntax of a value-returning method is:
modifier(s) returnType methodName(formal parameter list)
{
statements
}
In this syntax:
modifier(s)
indicates the visibility of the method, that is, where in a
program the method can be used (called). Some of the modifiers are
public
,
private
,
protected
,
static
,
abstract
, and
final
. If you
include more than one modifier, they must be separated with spaces. You
can select one modifier among
public
,
protected
, and
private
. The
modifier
public
specifies that the method can be called outside the class;
the modifier
private
specifies that the method cannot be used outside
the class. Similarly, you can choose one of the modifiers
static
or
Search WWH ::
Custom Search