Method calls, the details

Figure 309. Method calling Slide presentation Create comment in forum
public class Circle {
  static final double
    PI = 3.141592653589793;
  double r;
  /** Change a circle's area
   *  @param area The desired new area
   *  @return The circle's new radius */
  double setArea(final double area ) {
    double val  = area  / PI ;
    return  r  = Math.sqrt(val);
  }
}

Passing arguments.

Defining method local variables.

Accessing class variable.

returning values.

Accessing instance variable.


Figure 310. Three variable scopes Slide presentation Create comment in forum
public class Circle {

  static final double
    PI  = 3.141592653589793;

  double r  ;

  double setArea(final double area ) {
    double val  ...
    ...
  }
}

Class scope.

Instance scope

Method scope


Figure 311. Scope lifetimes Slide presentation Create comment in forum
Class scope (static) Application process
Instance scope Object lifetime: new (...) until being garbage collected.
Method scope During method call until return.

Figure 312. Two runtime memory categories Slide presentation Create comment in forum
Heap memory
  • Allocation by new Classname ().

  • De-allocation subject to garbage collection.

Execution stack
  • One instance per process thread.

  • Hosting variables (values or references)


Figure 313. Stack: Four operations Slide presentation Create comment in forum
Method Description Precondition Access type
push(...)

Add object on top

-

Modify

pop()

Read + remove topmost object

Stack not empty

Modify

top()

Read topmost object

Stack not empty

Read only

empty()

true if and only if stack is empty

-

Read only

The JDK implements a peek() method having exactly the «original» stack top() method's semantic.

Figure 314. Example: Storing integer values Slide presentation Create comment in forum

Figure 315. Method calling Slide presentation Create comment in forum

Figure 316. Call stack trace Slide presentation Create comment in forum

Figure 317. IDE debugger Slide presentation Create comment in forum
IDE debugger

Three call stack frames corresponding to main() calling circleArea(2) calling square(2).

Local variables corresponding to selected stack frame.

Tip

In the above example you may select stack frame circleArea showing its local variables while leaving your debugger resting at line 3.