Review of methods

  • Review: method definition:

      • Method definition specifies the statements that the method will execute.

  • Review: method invocation:

      • Method invocation will execute/run the statements in the body of the method

  • Review: How to invoke methods:

     Methods that return a value:
    
         z = max(x, y);       // max(x,y) is replaced by the return value 
    
    
    void typed methods: printMax(x,y);

Computer Science concept:   Program control

  • Program control:

      • Program control is the ability of a computer program to control the sequence of instructions that the computer program will perform.


  • Example of program control:   selection using an if-statement

     if ( a > b )
        result = a;   // <--- selects this if a > b is true
     else
        result = b;   // <--- selects this otherwise
      

    The computer program can select a branch of execution using an if-statement

  • Another example of program control:

    • Method invocation      (discussed next)

Program control in a method invocation

Demo program used in explaining the program control flow in a method invocation:

    public static void main(String[] args) 
    {
        int x, y, z;
        
        x = 3;
        y = 7;
        z = max(x,y);
    } 
    
    public static int max(int a, int b)
    {
        int result;
        
        if ( a > b )
            result = a;
        else
            result = b;
            
        return result;
    }  

DEMO: demo/06-methods/02-method/Max.java

Step in BlueJ     ---    Show variable content of main( ) while stopped in max( )

Program control in a method invocation

Detailed explanation:   when a Java program starts, it will call the main( ):

 

 The program starts execution at the first statement in the main( ) method.


  

Program control in a method invocation

When the program invokes/calls the max( ) method, program control is transfered to the called method max:

 

 (1A) The method call passes (=copies) the actual parameters to the formal parameters
 (1B) Program control is transfered to max

 (2) The method that makes the call is paused at the call statement method 

Program control in a method invocation

When the called method executes a return statement, program control is returned to the calling method:

 

 The method call is replaced by the return value 
   

 

Activation record

Each time a method is invoked, Java creates an activation record that stores parameters and variables:

 

 The activation record is placed in an area of memory known as a call stack.

 A call stack is also known as an execution stack, runtime stack,
 or machine stack, and it is often shortened to just "the stack". 

Activation record

The activation record remains on the stack as long as the method is active (= has not returned):

 

 main( ) is active when max( ) is being run !

 main( ) also has an activation record !

 Show in BlueJ !!!

Activation record

The activation record is deleted when a method returns :

 

 The activation record of max is gone in BlueJ when method returns