Method calls, the details

Figure 430. 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 431. 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 432. 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 433. 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 434. 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.