Example of a method definition and method invocation:
public class Max
{
public static void main(String[] args)
{
int z, x = 3, y = 7;
z = max(x,y); // Method invocation
}
public static int max(int a, int b) // Method definition
{
int result;
if ( a > b )
result = a;
else
result = b;
return result;
}
}
|
DEMO: demo/06-methods/02-method/Max.java
Demo: show the activation records of main( ) and max( ) when stopped inside max( )
Syntax and structure of a method definition and method invocation:
We will learn some computer jargon (terminology) about methods next
A method definition consists of (1) a method header and (2) a method body:
The method header consists of modifiers, return value type, method name, and (formal) parameters:
We will examine the different components in the method header next...
The public modifier declares that the method is accessible (= usable) by other classes:
A class can also have private methods which we will study later
The static modifier declares that the method is general purposed and does not operate on an object:
We will study static methods in this chapter (and instance methods in Chapter 8: classes and objects)
A method may return a value. The returnValueType specifies the data type of the value returned:
Some methods do not return any value. The return type of such method is: void (E.g.: void main( ))
The variables defined in the method header are known as formal parameters (or simply as parameters):
A parameter is a placeholder for an input value: you pass (= copy) a value to the parameter at invocation
The values passed to a method at invocation are called actual parameters (or: arguments):
The assignment from actual parameters to formal parameters is by their position in the parameter lists.
The parameter list is determined by (1) ordering, (2) number and (3) data type of the parameters:
Two parameter lists are different if any one of the three properties is different
The method name and the parameter list together constitute (= make up) the method signature:
In every class, you cannot have more than one (> 1) method with the same method signature.
The method body contains a collection of statements that implement the method:
The statements in the method body are executed when a method is invoked (or called)
To execute a method, you use a method invocation/call statement:
The program execution is then transfered to the method until the called method returns -- Demo in BlueJ
The return statement returns the return value to the method invocation statement:
The return value will replace the invocation upon return and the return value is used in further calculation
Methods that do not return a value are defined with returnValueType void:
public static void main(String[] args)
{
int x = 3, y = 7;
printMax(x,y); // How to invoke a void typed method
}
public static void printMax(int a, int b)
{
if ( a > b )
System.out.println(a);
else
System.out.println(b);
return; // Optional
}
|
When the method executes a return statement, it will return to the caller
When execution reaches the end of the method, it will also return to the caller
DEMO: demo/06-methods/02-method/PrintMax.java