Scopes

Figure 238. Circle and variable scopes Slide presentation

Figure 239. Documenting classes and methods Slide presentation
/** Representing circles.
 */
public class Circle {
  private double radius;

  /** Creating a circle.
   *  @param r representing the circle's radius
   */
  public Circle(double r) {
    radius = r;
  }
  public double getDiameter() {
    return 2 * radius;
  }
}

Figure 240. Generated Javadoc Slide presentation

Figure 241. Refactoring «r» ⇒ «radius» Slide presentation

Figure 242. Scope assignment problem Slide presentation
/** Representing circles.
 */
public class Circle {
  private double radius;

  /** Creating a circle.
   *  @param radius Circle's size
   */
  public Circle(double radius) {
    radius = radius; // Warning: Variable 'radius' is assigned to itself.
  }
  public double getDiameter() {
    return 2 * radius;
  }
}

Figure 243. this overriding method scope Slide presentation

exercise No. 103

Constructors variable names and this.

Q:

Currently our constructor is being implemented as:

public Employee(int ageValue, double salaryValue) {
  age = ageValue;
  salary = salaryValue;
}

This imposes problems with respect to proper documentation. A developer will try to choose reasonable variable names clearly indicating the desired purpose like age and salary.

The ratio for choosing ageValue and salaryValue originates from the need to resolve instance from method scope thereby avoiding shadowing of variable names. From the viewpoint of code comprehensibility we prefer:

public class Employee {
  int age, salary;   // Instance scope
  public Employee(int age /* method scope */, double salary /* method scope */) {
                     // Compiler warnings:
    age = age;       // The assignments to age and salary have no effect: The assignment operator`s
    salary = salary; // left hand side refers to the method parameters rather than to instance scope.
  }
}

This compiles correctly but yields two warnings: The constructor argument variables age and salary will be assigned to themselves. More important their values don't make it to the age and salary attributes being defined on instance level.

Apparently we are missing something. Explain these two compiler warnings. How can the underlying conflict be resolved?

Tip

Read the section in [Kurniawan] about the this keyword and the class vs. method scope slide.

A:

When choosing public Employee(int age, double salary) we have two sets of variables (age, salary) in two different conflicting scopes:

Instance scope:
public class Employee {
   public int age;
   public double salary;
  ...
}

Both variables are accessible from within the whole class including all its methods.

Method's formal parameter list scope:
public Employee(int age, double salary) {.../* Constructor's method body */}

Definition of these two variables is being limited to the method's body.

Within the constructor's method body the parameter list scope will take precedence over instance scope. Thus the assignment age = age will assign the constructor's argument age to itself rather than to the instance variable age being defined at class scope.

We may explicitly resolve this scope conflict in our favour by qualifying the instance variables age and salary using their respective scope being represented by the this keyword:

public Employee(int age, double salary) {
  // The "this" keyword refers to class scope
  this.age = age;
  this.salary = salary;
}