Method calls, the details

Figure 305. 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 306. 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 307. 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 308. 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 309. 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 310. Example: Storing integer values Slide presentation Create comment in forum

Figure 311. Method calling Slide presentation Create comment in forum

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

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