A method is a Java procedure. Methods provide the actions
in Java programs - they do things. They compute things.
They modify fields. They output words, graphics, sounds,
send messages over the Internet, an infinite number of
different things...
All methods have a type,
which means they either output a value of their declared
type or, if the type is "void," no output
is produced.
There are two parts to a method declaration:
- a header which introduces the method. A header
has the form:
modifiers type methodIdentifier ( parameters ) throws exception
where:
- modifiers are keywords like
public, private, abstract,
and static.
- type is a keyword for a Java
primitive type, the keyword
void, or a reference-type.
- methodIdentifier is the name you are giving
to the method being declared.
- ( parameters ) it the list of
parameters that the method is expecting
when it is
invoked. Although the parenthesis
MUST be there, parameter declarations
are needed only when the method needs
them.
- throws exception is
an optional declaration necessary when
the method could generate a checked
exception.
- a body. The body of a method is either simply
a semicolon, which means the body isn't implemented
at this point, or it's a
block.