Local Classes / Local Inner Classes

//Local Classes / Local Inner Classes
  1. Definition

  2. Scope of Local Classes

  3. Accessing members in Local classes

1. Local Class :

Sometimes we can declare a class inside any block such as instance block or constructor or method or if block, such type of inner classes are called local inner classes or local classes.

The main objective of local inner class is to define logic that required instantly to use in local scope.

2. Scope of Local classes :

We can access local class only within the method or block where we declared it. That is from outside of the block we can’t access. As the scope of local inner classes is very less, this type of inner classes are most rarely used type of inner classes. Mostly this kind of use cases we can see in multi threading programming.

3. Accessing members in local classes :

1. If we are declaring inner class inside instance block or method then we can access both static and non static members of outer class directly.
2. But if we are declaring inner class inside static method or block then we can access only static members of outer class directly and we can’t access instance members directly.
3. We can access constants directly inside local classes and we can declare also. A final variable is a constant, so that we can access final members directly.
4. Only allowed modifiers for local classes are final, abstract and strictfp.