Method calls, the details

Figure 339. Method calling Slide presentation
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 340. Three variable scopes Slide presentation
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 341. Scope lifetimes Slide presentation
Class scope (static) Application process
Instance scope Object lifetime: new (...) until being garbage collected.
Method scope Method invocation until return.

Figure 342. Two runtime memory categories Slide presentation
Heap memory
  • Allocation of class or array instances:

    new String()
    new float[200]
  • De-allocation subject to garbage collection.

Execution stack
  • One instance per process thread.

  • Hosting variables (values or references)


Figure 343. Stack: Four operations Slide presentation
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 344. Example: Storing integer values Slide presentation

Figure 345. Method calling Slide presentation

Figure 346. Call stack trace Slide presentation

Figure 347. IDE debugger Slide presentation
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.