Posted on Leave a comment

Scope Rules – Access Control

6.6 Scope Rules

Java provides explicit access modifiers to control the accessibility of members in a class by external clients, but in two areas access is governed by specific scope rules:

  • Class scope for members: how member declarations are accessed within the class
  • Block scope for local variables: how local variable declarations are accessed within a block

Class Scope for Members

Class scope concerns accessing members (including inherited ones) from code within a class. Table 6.4 gives an overview of how static and non-static code in a class can access members of the class, including those that are inherited. Table 6.4 assumes the following declarations:

Click here to view code image

class SuperClass {
  int instanceVarInSuper;
  static int staticVarInSuper;
void instanceMethodInSuper()      { /* … */ }
  static void staticMethodInSuper() { /* … */ }
  // …
}
class MyClass extends SuperClass {
  int instanceVar;
  static int staticVar;
  void instanceMethod()      { /* … */ }
  static void staticMethod() { /* … */ }
  // …
}

Table 6.4 Accessing Members within a Class

Member declarationsNon-static code in the class MyClass can refer to the member asStatic code in the class MyClass can refer to the member as
Instance variablesinstanceVar this.instanceVar instanceVarInSuper this.instanceVarInSuper super.instanceVarInSuperNot possible
Instance methodsinstanceMethod() this.instanceMethod() instanceMethodInSuper() this.instanceMethodInSuper() super.instanceMethodInSuper()Not possible
Static variablesstaticVar this.staticVar MyClass.staticVar staticVarInSuper this.staticVarInSuper super.staticVarInSuper MyClass.staticVarInSuper SuperClass.staticVarInSuperstaticVar MyClass.staticVar staticVarInSuper MyClass.staticVarInSuper SuperClass.staticVarInSuper
Static methodsstaticMethod() this.staticMethod() MyClass.staticMethod() staticMethodInSuper() this.staticMethodInSuper() super.staticMethodInSuper() MyClass.staticMethodInSuper() SuperClass.staticMethodInSuper()staticMethod() MyClass.staticMethod() staticMethodInSuper() MyClass.staticMethodInSuper() SuperClass.staticMethodInSuper()

The golden rule is that static code can only access other static members by their simple names. Static code is not executed in the context of an object, so the references this and super are not available. An object has knowledge of its class, so static members are always accessible in a non-static context.

Note that using the class name to access static members within the class is no different from how external clients access these static members.

The following factors can all influence the scope of a member declaration:

  • Shadowing of a field declaration, either by local variables (p. 354) or by declarations in the subclass (§5.1, p. 203)
  • Overriding an instance method from a superclass (§5.1, p. 196)
  • Hiding a static method declared in a superclass (§5.1, p. 203)

Within a class, references of the class can be declared and used to access all members in the class, regardless of their access modifiers. In Example 6.9, the method duplicateLight at (1) in the class Light has the parameter oldLight and the local variable newLight that are references of the class Light. Even though the fields of the class are private, they are accessible through the two references (oldLight and newLight) in the method duplicateLight(), as shown at (2), (3), and (4).

Example 6.9 Class Scope

Click here to view code image

class Light {
  // Instance variables:
  private int     noOfWatts;       // Wattage
  private boolean indicator;       // On or off
  private String  location;        // Placement
  // Instance methods:
  public void switchOn()  { indicator = true; }
  public void switchOff() { indicator = false; }
  public boolean isOn()   { return indicator; }
  public static Light duplicateLight(Light oldLight) {     // (1)
    Light newLight = new Light();
    newLight.noOfWatts = oldLight.noOfWatts;               // (2)
    newLight.indicator = oldLight.indicator;               // (3)
    newLight.location  = oldLight.location;                // (4)
    return newLight;
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *