Local Classes – Nested Type Declarations

9.4 Local Classes

Declaring Local Classes

A local class is an inner class that is defined in a block. This can be essentially any context where a local block or block body is allowed: a method, a constructor, an initializer block, a try-catch-finally construct, loop bodies, or an if-else statement. Example 9.9 shows declaration of the local class StaticLocal at (5) that is defined in the static context of the method staticMethod() at (1).

A local class cannot have any access modifier and cannot be declared static, as shown at (4) in Example 9.9. However, it can be declared abstract or final, as shown at (5). The declaration of the class is only accessible in the context of the block in which it is defined, subject to the same scope rules as for local variable declarations. In particular, it must be declared before use in the block. In Example 9.9, an attempt to create an object of class StaticLocal at (2) and use the class Static-Local at (3) fails, as the class has not been defined before use, but this is not a problem at (11), (12), (13), and (14).

A local class can declare members and constructors, shown from (6) to (10), as in a normal class. The members of the local class can have any access level, and are accessible in the enclosing block regardless of their access level. Even though the field if1 at (7) is private, it is accessible in the enclosing method at (12).

Blocks in non-static context have a this reference available, which refers to an instance of the class containing the block. An instance of a local class, which is declared in such a non-static block, has an instance of the enclosing class associated with it. This gives such a non-static local class much of the same capability as a non-static member class.

However, if the block containing a local class declaration is defined in static context (i.e., a static method or a static initializer block), the local class is implicitly static in the sense that its instantiation does not require any outer object. This aspect of local classes is reminiscent of static member classes. However, note that a local class cannot be specified with the keyword static. The static method at (1) is called at (15). The local class StaticLocal can only be instantiated, as shown at (11), in the enclosing method staticMethod() and does not require any outer object of the enclosing class. Analogous to the value of a local variable, the object of the local class is not available to the caller of the method after the method completes execution, unless measures are taken to store it externally or if its reference value is returned by the call.

Example 9.9 Declaring Local Classes

Click here to view code image

// File: LocalClient1.java
class TLCWithSLClass {                             // Top-level Class
  static void staticMethod(final int fp) {         // (1) Static Method
//  StaticLocal slRef = new StaticLocal(10);       // (2) Class cannot be resolved
//  System.out.println(StaticLocal.staticValue()); // (3) Class cannot be resolved

//  public static class StaticLocal { // (4) Not OK. Cannot be static,
                                      //             and no access modifier
    final class StaticLocal {                        // (5) Static local class
      public static final int sf1 = 10;              // (6) Static field
      private int if1;                               // (7) Instance field
      public StaticLocal(int val) {                  // (8) Constructor
        this.if1 = val;
      }
      public int getValue() { return if1; }          // (9) Instance method
      public static int staticValue() { return sf1; }// (10) Static method
    } // end StaticLocal
    StaticLocal slRef2 = new StaticLocal(100);                             // (11)
    System.out.println(“Instance field: ” + slRef2.if1);                   // (12)
    System.out.println(“Instance method call: ” + slRef2.getValue());      // (13)
    System.out.println(“Static method call: ” + StaticLocal.staticValue());// (14)
  } // end staticMethod
}
public class LocalClient1 {
  public static void main(String[] args) {
    TLCWithSLClass.staticMethod(100);                                      // (15)
  }
}

Output from the program:

Instance field: 100
Instance method call: 100
Static method call: 10

Leave a Reply

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