blog

Home / DeveloperSection / Blogs / Nested Classes in Java: Local Classes

Nested Classes in Java: Local Classes

Royce Roy 1720 19-May-2016
Member Class

A non-static class defined within a class is called a member class of the enclosing class. A member class is commonly used as a helper class to the enclosing class. A member class can access the instance fields of the enclosing class, whereas a nested top-level class cannot do so. To refresh our memory, a nested top-level class is an inner class declared with a static modifier. Therefore, if we want objects of an inner class to have access to the fields of the enclosing class, we will declare it as a member class rather than as a nested top-level class. All objects of a member class will have access to the same field of the enclosing class.

Here are the main features of member classes:

·   Every instance of a member class is internally associated with an instance of its outer class.

·   The methods of a member class can implicitly refer to the fields defined by the enclosing class, including those that are declared private.

Local Classes

A class declared within a block of Java code is called a local class. Typically, such a block would be a method, but local classes may also be declared within static initializers and constructors of a class. A local class declared within the constructor of the enclosing class is shown here

public class OuterClass {
                     public OuterClass() {
                             class Local {
                                      public Local() {
                                                // local class constructor code here                                       }
                             }
                             new Local();
                     }
                     public void instanceMethod() {
                             new OuterClass();
                     }
}

 The constructor instantiates the Local class and uses this object within its scope. The instanceMethod is the member method of the OuterClass that creates an instance of it. During this instantiation, a copy of the Local object would be created within the scope of the constructor.

Defining an Inner Class within Method Scope

The inner class may also be defined within a method. In this case, the visibility of the inner class is restricted to the method scope. The use of an inner class declaration within a method body is illustrated with a trivial example here.

class Outer {
                     private int a = 20;
                     public void someMethod(final int b) {
                             class Inner {
                                      int c = 30;
                                      public void innerMethod() {
                                                System.out.println("Formal parameter (B): " + b);                                                 System.out.println("Outer Class variable (A): " + a);                                                 System.out.println("Inner Class variable (C): " + c);                                       }
                             }
                             new Inner().innerMethod();
                     }
}
public class InnerClassWithinMethodExample {
                     public static void main(String[] args) {
                             Outer outer = new Outer();
                             outer.someMethod(10);
                     }
}

The Outer class defines a method called someMethod that takes a parameter of type int. Within the method body, we declare an inner class called Inner. The inner class declares a local variable, c, and defines a method called innerMethod. The method implementation accesses the formal parameter passed to the enclosing method and also the variables declared in the Outer and Inner classes. The main method of the application creates an instance of the Outer class and calls someMethod on it. someMethod in turn creates an Inner object and calls the method innerMethod on it. This outputs the three values, shown next, on the screen: 

Formal parameter (B): 10
Outer Class variable (A): 20
Inner Class variable (C): 30

 

Few Important Points on Local Classes

Here’s a list of the important features of local classes:

  • A local class is only visible and usable within the block of code in which it is defined.
  • In addition to accessing fields defined by the containing class, local classes can access any local variables, method parameters, or exception parameters that are in the scope of the local method definition, provided they are declared with the final specifier.
  • Local classes cannot use the new and super keywords.
  • Local classes cannot contain fields, methods, or classes that are declared static. Because nested interfaces are implicitly static, local classes may not contain nested interface definitions.
  • Local classes cannot be declared with the modifiers public, protected, private, and static. These modifiers are used only on members of classes and are not allowed on local class declarations.
  • A local class cannot have the same name as any of its enclosing classes.
  • Interfaces cannot be defined locally.
  • A local class can use any final local variables or method parameters that are visible from the scope in which they are defined.

Updated 15-Mar-2018

Leave Comment

Comments

Liked By