Method local inner class
=====================================
- Sometimes we can declare a class inside a method, such type of inner classes are called method local inner class.
- The main purpose of method local inner class is to define method specific repeated functionality.
- Method local inner classes are best suitable to meet nested method requirements.
- We can access method local inner classes only within the method where we declare.
- Outside of the method we can't access.
- Because of it's less scope, method local inner classes are most rarely used type of inner class.
Ex.
- We can declare method local inner class inside both instance and static method.
- If we declare inner class inside instance method then from that method local inner class can access both static and non static members of outer class directly.
- If we declare inner class inside static method then we can access only static members of outer class directly from that method local inner class.
Ex 1.
Ex 2.
The only applicable modifiers for method local inner classes are:
If we are trying to apply any other modifier then we will get compile time error.
Related topic
- Inner class
- Normal / Regular inner class.
- Method local inner class.
- Anonymous inner class.
- Static nested class.