Java Reference
In-Depth Information
ȗ A list of the parameters of the method (if any), enclosed in parentheses (such
as
double amount
)
ȗ The body of the method: statements enclosed in braces
The access specifier controls which other methods can call this method. Most
methods should be declared as
public
. That way, all other methods in a program
can call them. (Occasionally, it can be useful to have
private
methods. They can
only be called from other methods of the same class.)
A method definition contains an access specifier (usually
public
), a return type,
a method name, parameters, and the method body.
The return type is the type of the output value. The
deposit
method does not return
a value, whereas the
getBalance
method returns a value of type
double
.
86
87
S
YNTAX
3.1 Method Definition
accessSpecifier returnType
methodName(parameterType parameterName, . . . )
{
method body
}
Example:
public void deposit(double amount)
{
. . .
}
Purpose:
To define the behavior of a method
Each parameter (or input) to the method has both a type and a name. For example, the
deposit
method has a single parameter named
amount
of type
double
. For each
parameter, choose a name that is both a legal variable name and a good description of
the purpose of the input.