Methods contain code that would otherwise have to be repeated time and again elsewhere, perhaps only with different values plugged into it. So if you find yourself having to write very similar code time and again, you should make a method that can be called from where it is needed.
Method is Java's term for what in other languages may be called function, procedure or subroutine.
When we come to classes and objects, methods define the behaviour.
Syntax:
modifiers returnType methodName (parameters)
{
statements
} // methodName
As we have already seen, modifiers
include such keywords as public
, static
, final
. Another one is private
, as an alternative to public
, meaning that the method can only be called from within the class where it is defined.
returnType
can be any of the primitive types or (later) a class type - meaning that the method will return a value of that type to the caller when it is finished. If the method will not return anything we put the keyword void
as the returnType
.
methodName
must start with a lower case letter.
parameters
may or may not be present. If there are none, the parentheses, (), must still be used. Parameters form a comma-separated list, each one comprising 2 parts: a type name followed by a variable name. The variable name is the name by which the parameter will be known inside the method.
Example:
public int average (int a, int b, int c, int d)
{
int sum = a + b + c + d;
return sum / 4; // Must have a return statement if the return type is not void
} // average
This method, as its name suggests, averages 4 numbers of type int
.
NB: Local copies of the parameters are used inside the method; changing them does not affect external originals.
Call (invoke) it by giving its name and the required number of parameters, which may be values or expressions, as long as they are of the right type. If the method returns something you can optionally (and would usually) assign the returned value to a variable of that type or use it as a parameter to another method call.
Example:
int average = average (i1, i2, i3, i4);
Yes, a variable and a method can have the same name, as in this example, but it is NOT good practice. Think of different names. Since the method is actively doing something it might have been better to have named this one averager ()
.
There can be several methods with the same name if they differ in parameter types and/or number of parameters. Eg, the following could all exist in the same class:
public int average (int a, int b, int c)
{ /*...*/ }
public int average (int a, int b)
{ /*...*/ }
public float average (float a, float b, float c)
{ /*...*/ }
This is called overloading of methods (not to be confused with another term we will come to later, called overriding).
Differing return type is not enough - compiler will reject.
Since Java 5 some need for overloading has been reduced by a new bit of syntax:
public int average (int a...)
{ /*...*/ }
means that any number of parameters of type int
may be passed in. But then you need to know how to read and process them as arrays, and we will come to that later.