Methods

A method is a part of a program. It offers a specific functionality. A program is made of many methods. In other languages methods are called subroutines, procedures or functions.

A method has a method header and a method body. The header of a method has the following format:

[Method Modifiers] ReturnType MethodName ([Parameter List])
The terms in the square brackets [] are optional. Hence, at the least a method header must have the ReturnType and MethodName. Java defines method signature to be the name of the method and the parameter list (the order of the parameters in that list is important).
public static int max ( int x, int y )

Some of the modifiers that we have learnt of so far are public and static. A public method can be accessed outside of the class, if the class has also been declared public. The static modifier allows the method to be accessed outside the class without creating an object of that class. For example, all the methods in the Math class have been declared static. We access those methods by calling on the name of the class and the method name - Math.sqrt(d) or Math.pow (a, b).

If the method returns some value, then the ReturnType of the method is the type of the value that is being returned. For example, if the method returns an integer after the result of some computation, then the ReturnType of the method is int. Methods that do not return any value have a ReturnType of void.

In Java we use the convention that method names start with a small letter. If the method name is two words then the first letter of the second word is capitalized. Method names may not be unique but method signatures must be unique.

The Parameter List is optional. Even if there are no parameters, the method signature must have the open and close parenthesis (). The parameters declared in the method signature are known as formal parameters. The order, the types, and the number of parameters are important.

The body of the method is enclosed in {}. Another term for the body of the method is the implementation of the method. The body of the method defines the method.

Calling a Method

Inside the class that defines the method, the method could be called by name. Outside the class, public methods may be accessed by first creating an object of that class and then calling the method. However, if a method has been declared static you can call the method by using ClassName.methodName().

If a method has a return type then the value gets assigned to a variable of the same type.

int a = 2;
int b = 3;
int localMax = max (a, b);
You can also print out the return value without assigning it to a variable.
System.out.println ("The maximum is " + max (a, b));

A method that has a return of void is just called by name.


public static void printMax ( int x, int y )
{
  ...
}

...
...

printMax (a, b);

Passing Parameters

The variables that get mapped to the parameter list of a method are known as the actual parameters. The actual parameters must match the order, the type and the number of the formal parameter list.

Pass by value

Primitive types are passed by value to a method. If the actual parameter is of a primitive type, then a copy of its value is made and assigned to the corresponding formal parameter in the method signature. Changes of the value of the formal parameter will not change the value of the actual parameter after the execution of the method.
public static void swap ( int a, int b )
{
 int tmp = a;
 a = b;         //  a has the value of b
 b = tmp;       //  b has the value of a
}

int x = 4;
int y = 7;
swap (x, y);

//  x is still 4 and y is still 7

Method Overloading

As we stated earlier that the method name may not be unique but the method signature has to be unique. Java will permit the following definitions of these two methods in the same program.

public static int max ( int x, int y ) { ... }

public static double max ( double x, double y ) { ... }

You want to overload the names of the methods only if they offer the same functionality. Both the methods max return the maximum of two numbers.

Scope rules

In Java a variable has to be declared before it can be used. The scope of a variable starts from its declaration to the closing brace of the block within which it has been declared. Local variables within a method can not be accessed outside the method. If we have two variables with the same name (though not defined in the same block) then the local variable takes precedence.
public class SomeClass
{
  public boolean found;

  public static void someMethod ()
  {
    int found = 1;
    ...
  }

  public static double someOtherMethod ()
  {
   ...
  }

  ...
}
The scope of the boolean variable found can be referenced or accessed throughout the class. For example the method someOtherMethod() can access the value of the boolean variable found. However, in the method someMethod() found is of type integer and has a value of 1.