Defining and invoking a method

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( )

Defining and invoking a method

Syntax and structure of a method definition and method invocation:

We will learn some computer jargon (terminology) about methods next

Defining and invoking a method

A method definition consists of (1) a method header and (2) a method body:

 

Defining and invoking a method

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...

Defining and invoking a method

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

Defining and invoking a method

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)

Defining and invoking a method

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( ))

Defining and invoking a method

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

Defining and invoking a method

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.

Defining and invoking a method

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

Defining and invoking a method

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.

How a method is used

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)

How a method is used

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

How a method is used

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

Example of a void typed method

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