Accessing Declarations in Enclosing Context – Nested Type Declarations

Accessing Declarations in Enclosing Context

Declaring a local class in a static or a non-static block influences what the class can access in the enclosing context.

Accessing Local Declarations in the Enclosing Block

Example 9.10 illustrates how a local class can access declarations in its enclosing block. Example 9.10 shows declaration of the local class NonStaticLocal at (7) that is defined in the non-static context of the method nonStaticMethod() at (1).

A local class can access variables (local variables, method parameters, and catch-block parameters) that are declared final or effectively final in the scope of its local context. A variable whose value does not change after it is initialized is said to be effectively final. This situation is shown at (8) and (9) in the NonStaticLocal class, where the final parameter fp and the effectively final local variable flv of the method nonStaticMethod() are accessed. Access to local variables that are not final or effectively final is not permitted from local classes. The local variable nfv1 at (4) is accessed at (10) in the local class, but this local variable is not effectively final as it is reassigned a new value at (6).

Accessing a local variable from the local context that has not been declared or has not been definitely assigned (§5.5, p. 232) results in a compile-time error, as shown at (11) and (12). The local variable nflv2 accessed at (11) is not declared before use, as it is declared at (16). The local variable nflv3 accessed at (12) is not initialized before use, as it is initialized at (17)—which means it is not definitely assigned at (12).

Declarations in the local class can shadow declarations in the enclosing block. The field hlv at (13) shadows the local variable by the same name at (3) in the enclosing method. There is no way for the local class to refer to shadowed declarations in the enclosing block.

The non-static method at (1) is called at (19) on an instance of its enclosing class. When the constructor at (15) in the non-static method is executed, the reference to this instance is passed implicitly to the constructor, thus this instance acts as the enclosing object of the local class instance.

Example 9.10 Accessing Local Declarations in the Enclosing Block (Local Classes)

Click here to view code image

// File: LocalClient2.java
class TLCWithNSLClass {                // Top-level Class
  void nonStaticMethod(final int fp) { // (1) Non-static Method
    // Local variables:
    int flv = 10;              // (2) Effectively final local variable
    final int hlv = 20;        // (3) Final local variable (constant variable)
    int nflv1 = 30;            // (4) Non-final local variable
    int nflv3;                 // (5) Non-final local variable declaration
    nflv1 = 40;                // (6) Not effectively final local variable
    // Non-static local class
    class NonStaticLocal {// (7)
      int f1 = fp;        // (8) Final param from enclosing method
      int f2 = flv;       // (9) Effectively final variable from enclosing method
//    int f3 = nflv1;     // (10) Not effectively final from enclosing method
//    int f4 = nflv2;     // (11) Name nflv2 cannot be resolved: use-before-decl
//    int f5 = nflv3;     // (12) Not definitely assigned
      int hlv;            // (13) Shadows local variable at (3)
      NonStaticLocal (int value) {
        hlv = value;
        System.out.println(“Instance field: ” + hlv);// (14) Prints value from (13)
}
    } // end NonStaticLocal
    NonStaticLocal nslRef = new NonStaticLocal(200);// (15) Implicit outer object
    int nflv2 = 50;                                 // (16) Attempted use in (11)
    nflv3 = 60;                                     // (17) Initializes (4)
    System.out.println(“Local variable: ” + hlv);   // (18) Prints value from (3)
  } // end nonStaticMethod
}
public class LocalClient2 {
  public static void main(String[] args) {
    new TLCWithNSLClass().nonStaticMethod(1000);    // (19)
  }
}

Output from the program: Instance field: 200
Local variable: 20

Leave a Reply

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