A class definition typically includes one or more methods, which
carry out some action with the data. Methods resemble the functions
and subroutines in other languages. A method will be called, or
invoked, by code in some other method. (Remember that in
Java all processing occurs within the class framework.)
A method may return a value. For example, the code listing here
shows a class with one method that returns an integer value and
one that returns a double value. The set
(int j) method, on the other hand, does not return a value.
Such a method uses the void
return type modifier.
public
class
GenericClass
{
int i;
public
int get ()
{
return
i;
}
public
void set (int j)
{
i = j;
}
public double triple
(int j)
{
double
f = 3.0 * j * i;
return
f;
}
}
|
Field with a declaration of an integer data variable.
A method to obtain the value of the i variable.
A method to set the value of the i variable. Argument defines
type for value passed.
A method with local variable.
Local variable valid only within the method. It must be
assigned a value before it is used.
Return the calculated value.
|
The structure of a method includes a method signature
and a code body::
access
modifier return type method name (list of
arguments)
{
statements, including local variable
declarations
}
The first line shows a method signature consisting of
- access modifier
- determines what other classes and subclasses can invoke
this method. We will discuss access
modifiers in Chapter 5.
- return type
- what primitive or class type value will return from the
invocation of the method. In the above get()
method, for example, the return type is int.
If there is no value return, use void
for the return type as in the set(int
j) method above.
- method name
- follows the same identifier
rules as for data names. Customarily, a method name begins with
a lower case letter.
- list of
arguments -
the values passed to the method. Listed with type and name as
in the (int
j) in the above set
method.
The code body, delineated by the brackets, includes:
- local variables
- data variables can be declared and used within the method. The
values are discarded when the process returns from the method.
- statements
- the code to carry out the task for the particular method
The code can also access other data besides the local variables.
So far we have discussed three locations where a method can access
data:
- class member data - the data defined in the class
definition fields
- local variables - data declared within the method and
only valid there.
- method arguments - data passed in the method argument
list. (These are essentially local variables. See the discussion
of value vs reference.)
For example, the above "triple"
method includes all three types of data : class member data (i),
local variables (f),
and the argument variables (j)
.
Two other kinds of data include data inherited when extending another
class (Chapter 4 discusses
extending classes) and referencing instance and static
data in other classes. We discuss references
later in this chapter.
Later we will discuss overloading,
in which methods in a class can have the same name but different
argument lists and return types.
References
& Web Resources
- Exceptions - a method can also
include a "throws"
clause in the method signature. This is used to indicate that
the code body includes a throw
statement that generates an exception to be passed up to the method
that invoked this method.
Latest update: Oct. 16, 2004
|