Java Basics

Methods 7 - How call works

Purpose of this lesson: New Java language features

The table below shows how the call stack changes as calls and returns in the KmToMilesMethods program are made. This shows the first 8 changes to the call stack after main is entered.

Dynamic changes in the call stack memory allocation

The table below shows how the call stack changes as calls and returns in the KmToMilesMethods program are made. This shows the first 8 changes to the call stack after main is entered.

There is actually something before main on the call stack, and the library methods that are called call many methods of their own, which isn't shown here because we don't need to know what they call.

Stack frame. Each box represents the information that's stored on the call stack for each method. This block of information is often called a stack frame. There is internal information associated with the method, for example, it saves the place to resume execution in the calling method. Each stack frame is labelled with the method name and a list of parameters and local variables that are allocated on the stack. "???" is written when we don't know (or care) what the local variables are that are used by a library method.

12345678
main
args
kms
miles
main
args
kms
miles
getDouble
prompt
str
main
args
kms
miles
getDouble
prompt
str
showInputDialog
???
main
args
kms
miles
getDouble
prompt
str
main
args
kms
miles
getDouble
prompt
str
parseDouble
???
main
args
kms
miles
getDouble
prompt
str
main
args
kms
miles
main
args
kms
miles
convertKmToMi
kilometers
miles

Typical call sequence

  1. Evaluate arguments left-to-right. If an argument is a simple variable or a literal value, there is no need to evaluate it. When an expression is used, the expression must be evaluated before the call can be made.
  2. Push a new stack frame on the call stack. When a method is called, memory is required to store the following information.
    • Parameter and local variable storage. The storage that is needed for each of the parameters and local variables is reserved in the stack frame.
    • Where to continue execution when the called method returns. You don't have to worry about this; it's automatically saved for you.
    • Other working storage needed by the method may be required. You don't have to do anything about this because it's handled automatically.
  3. Initialize the parameters. When the arguments are evaluated, they are assigned to the local parameters in the called method.
  4. Execute the method. After the stack frame for this method has been initialized, execution starts with the first statement and continues as normal. Execution may call on other methods, which will push and pop their own stack frames on the call stack.
  5. Return from the method. When a return statement is encountered, or the end of a void method is reached, the method returns. For non-void methods, the return value is passed back to the calling method. The stack frame storage for the called method is popped off the call stack. Popping something off the stack is really efficient - a pointer is simply moved to previous stack frame. This means that the current stack frame can be reused by other methods. Execution is continued in the called method immediately after where the call took place.

Review Questions

The table above shows only the first part of the execution. Show how the call stack would change during the remainder of the execution of the program.