Accessing Members in the Enclosing Class – Nested Type Declarations

Accessing Members in the Enclosing Class

Example 9.11 illustrates how a local class can access members in its enclosing class. The top-level class TLCWith2LCS declares two methods: nonStaticMethod() and staticMethod(). Both methods define a local class each: NonStaticLocal at (1) in non-static context and StaticLocal at (8) in static context, both of which are subclasses of the superclass Base.

A local class can access members inherited from its superclass in the usual way. The field nsf1 in the superclass Base is inherited by the local subclass NonStaticLocal. This inherited field is accessed in the NonStaticLocal class, as shown at (2), (3), and (4), by using the field’s simple name, the standard this reference, and the super keyword, respectively. This also applies for static local classes, as shown at (9), (10), and (11).

Fields and methods in the enclosing class can be hidden by member declarations in the local class. The non-static field nsf1, inherited by the local classes, hides the field by the same name in the enclosing class TLCWith2LCS. The qualified this can be used in non-static local classes for explicit referencing of members in the enclosing class, regardless of whether these members are hidden or not.

Click here to view code image

double f4 = TLCWith2LCS.this.nsf1;      // (5) In enclosing object.

However, the special form of the this construct cannot be used in a local class that is declared in static context, as shown at (12), since it does not have any notion of an outer object. A local class in static context cannot refer to non-static members in the enclosing context.

A non-static local class can access both static and non-static members defined in the enclosing class. The non-static field nsf2 and static field sf are defined in the enclosing class TLCWith2LCS. They are accessed in the NonStaticLocal class at (6) and (7), respectively. The special form of the this construct can also be used in non-static local classes, as previously mentioned.

However, a local class that is declared in static context can only directly access static members defined in the enclosing class. The static field sf in the class TLCWith2LCS is accessed in the StaticLocal class at (14), but the non-static field nsf1 cannot be accessed, as shown at (13).

Example 9.11 Accessing Members in the Enclosing Class (Local Classes)

Click here to view code image

// File: LocalClient3.java
class Base { protected int nsf1; }     // Superclass
//_______________________________________________________________________________
class TLCWith2LCS {                    // Top-level Class
  private int nsf1;                    // Non-static field
  private int nsf2;                    // Non-static field
  private static int sf;               // Static field
  void nonStaticMethod( int fp) {      // Non-static Method
    class NonStaticLocal extends Base {// (1) Non-static local subclass
      int f1 = nsf1;                   // (2) Inherited from superclass.
      int f2 = this.nsf1;              // (3) Inherited from superclass.
      int f3 = super.nsf1;             // (4) Inherited from superclass.
      int f4 = TLCWith2LCS.this.nsf1;  // (5) In enclosing object.
      int f5 = nsf2;                   // (6) Instance field in enclosing object.
      int f6 = sf;                     // (7) static field from enclosing class.
    } // NonStaticLocal
  } // nonStaticMethod
  static void staticMethod(final int fp) { // Static Method
    class StaticLocal extends Base {   // (8) Static local subclass
      int f1 = nsf1;                   // (9) Inherited from superclass.
      int f2 = this.nsf1;              // (10) Inherited from superclass.
      int f3 = super.nsf1;             // (11) Inherited from superclass.
//    int f4 = TLCWith2LCS.this.nsf1;  // (12) No enclosing object.
//    int f5 = nsf2;                   // (13) No enclosing object.
      int f6 = sf;                     // (14) static field from enclosing class.
    } // StaticLocal
  } // staticMethod
}
public class LocalClient3 {
  public static void main(String[] args) {
    TLCWith2LCS.staticMethod(200);          // (15)
    new TLCWith2LCS().nonStaticMethod(100); // (16)
  }
}

Leave a Reply

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