Method Local Inner Class in Java | Example Program

An inner class that is declared inside a method of the outer class is called method local inner class in Java. Its scope is limited to the block of a method in which it is declared.

Therefore, the declaration of method local inner class cannot use any access modifiers such as public, protected, private, and non-access modifiers such as static.

Method local inner class in Java can also be declared inside the constructor, static initializers, and non-static initializers.

The only applicable modifiers for the method local inner class are final, abstract, and strictfp.

Look at the below figure where we have declared an inner class inside a method m1() of outer class. The declaration of inner class inside a method of outer class is called method local inner class in Java.

The applicable modifiers with an inner class inside a method are shown in the below figure.

Method local inner class in java

How to instantiate Method local Inner class in Java?


Since an inner class is defined inside the method of an outer class, therefore, an instance or object of the local inner class should be created in the same method in which a local inner class has been declared.

A local inner class is called when that method is called. It cannot be instantiated from outside the block of the method where it is created.

We can access method local inner class only within the method where we declare. Outside of the method, we cannot access it because it is local to that method.

Therefore, a method local inner classes are the most rarely used in all types of the inner class because of its less scope.


Let’s take a simple example program and understand how to instantiate an object of the local inner class when it is declared inside a method of outer class.

Program source code 1:

package innerClass; 
public class OuterClass 
{ 
// An instance method of class OuterClass. 
   public void display()
  { 
// Declaration of a method local inner class. 
    class InnerClass 
    { 
       public void msg(){ 
        System.out.println("I am in Local inner class"); 
       } 
     } 
// Now, create an instance of a method local inner class and call the msg() method using object reference variable ic. 
     InnerClass ic = new InnerClass(); 
       ic.msg(); 
  } 
public static void main(String[] args) 
{ 
// Create an object of the outer class OuterClass. 
     OuterClass oc = new OuterClass(); 
       oc.display(); 
   } 
}
Output: 
       I am in Local inner class

Why do we need Method Local Inner class in Java?


The purpose or need of a method local inner class in Java is to define a specific method whose functionality is required repeatedly.

A method-local inner class is the best suitable to meet nested method requirements. Let’s understand this concept by taking a suitable example program.

Suppose we declare a class Test. Inside the class, we have declared a method m1(). Now inside the method m1(), we have declared a method sum() with two parameters x and y whose data types are int.

Assume that we have to calculate the sum of two numbers with different four sets of two values. Look at the below code. Is this code, right


Program source code 2:

package innerClass; 
public class Test1 
{ 
  public void m1()
  { 
    void sum(int x, int y)
    { 
     System.out.println("Sum of two numbers: " +(x+y)); 
    } 
   } 
public static void main(String[] args)
{ 
   Test1 t = new Test1(); 
    t.m1(); 
    t.sum(20,30); 
    t.sum(10,40); 
    t.sum(50,50); 
    t.sum(100,100); 
  } 
}

May be your answer will be yes. We can declare a method inside the m1() and can call that method based on our requirements because the best reusable component is a method only. But the code is wrong. why?

This because it is not possible to define a method inside another method in java. Java does not support the nested method concept. In this case, how will we fix this code?

There are two ways to fix this code.

First way: Declare this method at the class level and call the sum method with different values but if we assume that this sum functionality is required only for the m1().

Outside the m1(), it is not required. So, we cannot declare at the class level. In this case, we will go for the second way.

Second way: Inside a method, we cannot declare a method but we can declare a local inner class within a method. Inside the inner class, we will place sum() and call with different sets of values. Look at the source code to understand better.

Program source code 3:

package innerClass; 
public class Test1 
{ 
 public void m1(){ 
// Method-local inner class starts here. 
 class InnerClass { 
    public void sum(int x, int y){ 
    System.out.println("Sum of two numbers: " +(x+y)); 
  } 
 } 
InnerClass ic = new InnerClass(); 
  ic.sum(10, 20); 
  ic.sum(20, 30); 
  ic.sum(30, 40); 
  ic.sum(40, 50); 
} 
public static void main(String[] args)
{ 
 Test1 t = new Test1(); 
  t.m1(); 
 } 
}
Output: 
       Sum of two numbers: 30 
       Sum of two numbers: 50 
       Sum of two numbers: 70 
       Sum of two numbers: 90

Thus, we can solve the problem of repeated functionality inside a particular method.

How to access Instance variables of Outer class & local variables of method local inner class?


Since the object of an inner class exists within an object of its outer class. Therefore, a local inner class can access the instance variables of its outer class as well as local variables which are in scope.

This is because instance variables of the outer class exist throughout the life cycle of the object of the method local inner class.

Let’s take an example program to understand these concepts.

Program source code 4:

package innerClass; 
public class AccessingVar 
{ 
// Instance variables of outer class. 
    int a = 20; 
    static int b = 30; // Static variables of outer class. 
// Instance method of the outer class. 
  public void m1()
  { 
// Instance area. 
   class InnerClass 
   { 
// Local variables of inner class. 
    int c = 30; 
    public void printSum(){ 
       System.out.println("Sum of three numbers: " +(a+b+c)); // Accessing instance, static, and local variables inside the method local inner class. 
    } 
   } 
  InnerClass ic = new InnerClass(); 
    ic.printSum(); 
 } 
public static void main(String[] args) 
 { 
// Create an object of the outer class. 
  AccessingVar av = new AccessingVar(); 
   av.m1(); 
  } 
}
Output: 
       Sum of three numbers: 80

Declaration of Method local inner class inside Instance & Static method


We can declare a method local inner class in both instance method and static method but there is a small difference between them. let’s take an example program to understand the difference between them.

Program source code 5:

package innerClass; 
public class OuterClass1 
{ 
  int x = 10; // Instance variable of class OuterClass1. 
  static int y = 20; // Static variable of class OuterClass1. 
// Declaration of the static method. 
  static void m3() { 
    System.out.println("I am in OuterClass1 static method"); 
  } 
// Declaration of an instance method. 
   public void m1() { 
// Instance area of m1(). 
// Method local inner class starts here. 
  class InnerClass1 { 
    public void m2() 
    { 
      System.out.println("Value of x: " +x); 
      System.out.println("Value of y: " +y); 
       m3(); 
    } 
   } 
// Now, create an object of class InnerClass1. 
   InnerClass1 ic = new InnerClass1(); 
    ic.m2(); 
  } 
public static void main(String[] args) 
{ 
 OuterClass1 oc = new OuterClass1(); 
  oc.m1(); 
 } 
}
Output: 
       Value of x: 10 
       Value of y: 20 
       I am in OuterClass1 static method

Key point:

If you declare inner class directly inside an instance method, you can directly access both static and non-static members from the method local inner class.

Program source code 6:

package innerClass; 
public class OuterClass2 
{ 
  int x = 10; // Instance variable of class OuterClass1. 
  static int y = 20; // Static variable of class OuterClass1. 

// Declaration of the static method. 
  static void m3()
  { 
    System.out.println("I am in OuterClass1 static method"); 
  } 
// Declaration of a static method in OuterClass2. 
  public static void m1() { 
// This is a static area of m1(). So we can call only static members. 
// Method local inner class starts here. 

   class InnerClass1 
   { 
     public void m2() { 
   // System.out.println("Value of x: " +x); // Say line (1). 
      System.out.println("Value of y: " +y); 
        m3(); 
     } 
   } 
  InnerClass1 ic = new InnerClass1(); 
   ic.m2(); 
 } 
public static void main(String[] args) { 
 OuterClass2.m1(); 
  } 
}
Output: 
       Value of y: 20 
       I am in OuterClass1 static method

Since the m1() method is declared as static, at line 1, we will get compile-time error: cannot make a static reference to the non-static field x.

Key points:

1. If you declare a method local inner class inside a static method of the outer class, you can access only static members of the outer class directly from the method local inner class.

2. You cannot access instance members from the static area. This is the difference between the declaration of a method local inner class inside the instance and static method.

Can we access Local variables of method from Method Local Inner class?


A method local inner class cannot access the local variables of method in which it is declared because the local variables in a method exist only during the execution of that method.

That is, the local variables will be created while executing the method. Once the method execution is over, all the local variables become destroyed and inaccessible.

Let’s take an example program to understand the above concept.

Program source code 7:

package innerClass; 
public class AccessingLocalVar 
{ 
 public void m1()
 { 
  int x = 20; // local variable of m1(). 
  class InnerClass 
  { 
   public void printSum() { 
    System.out.println(x); // Compile time error. 
    } 
   } 
 InnerClass ic = new InnerClass(); 
    ic.printSum(); 
 } 
public static void main(String[] args) { 
 AccessingLocalVar av = new AccessingLocalVar(); 
  av.m1(); 
 } 
}
Output: 
        Compile time error: local variable x is accessed from within inner class; needs to be declared final.

Key points:

If you want to access the local variable within a method local inner class, the variable must be effectively final. An effectively final variable is a variable whose value does not change once it is initialized.

To have an effectively final variable, declare the variable as final. Let’s create a program based on it.

Program source code 8:

package innerClass; 
public class AccessingLocalVar 
{ 
 public void m1()
 { 
  final int x = 20; // local variable of m1(). 
  class InnerClass 
  { 
   public void printSum(){ 
   System.out.println(x);  
  } 
 } 
InnerClass ic = new InnerClass(); 
  ic.printSum(); 
 } 
public static void main(String[] args) { 
 // Create an object of the outer class. 
    AccessingLocalVar av = new AccessingLocalVar(); 
     av.m1(); 
   } 
}
Output: 
        20

Every final variable will be replaced by a value at the time of compilation only. That is ‘x’ will be replaced by a value 20 at compile time. Hence, we will not get compile-time error.

Note: Prior to Java 1.8 version, a local variable must be declared as final if it is accessed inside a local inner class or an anonymous inner class.

But from Java 1.8 version, this rule has been changed: the local variable need not be declared final, but it should be effectively final. Let’s create a program based on it.

Program source code 9:

package innerClass; 
public class AccessingTest 
{ 
  int a = 20; 
  static int b = 30; 
 AccessingTest()
 { 
    int c = 40; 
    final int d = 50; 
  class InnerClass 
  { 
  public void printValue()
  { 
    ...... 
  } 
 } 
 InnerClass ic = new InnerClass(); 
  ic.printValue(); 
 } 
 public static void main(String[] args) 
 { 
   AccessingTest obj = new AccessingTest(); 
 } 
}

Consider the following code. Which of the following variables can be accessed directly inside the method printValue()?

Ans: We can directly access variables a, b, c, and d inside the printValue(). The output will be 20, 30, 40, and 50.

If you are using version prior to Java 1.8 version, you cannot access the variable c. It will generate compile-time error.

Program source code 10:

public class AccessingTest 
{ 
  int a = 20; 
  static int b = 30; 
 public static void m1()
 { 
   int c = 40; 
   final int d = 50; 
  class InnerClass 
  { 
    public void printValue()
    { 
      ....... 
    } 
  } 
 InnerClass ic = new InnerClass(); 
  ic.printValue(); 
 } 
public static void main(String[] args) 
{ 
  AccessingTest obj = new AccessingTest(); 
    m1(); 
  } 
}

Consider the above program. Which of the following variables we can access directly inside the local inner class?

Ans: The variables b, and d can be accessed directly if you are using version prior to Java 1.8 version. The result will be 30, and 50.

But if you are using Java 1.8 version, the result will be 30, 40, and 50.


Hope that this tutorial has covered almost all important topics based on method local inner class in Java with example programs. I hope that you will have understood the concepts of method local inner class.
Thanks for reading!!!Next ⇒ Anonymous inner class in Java⇐ PrevNext ⇒

Leave a Comment