Method Local Inner Class in Java

A local inner class (local class) is a class declared inside a method, constructor, instance initializer block, or static initializer block of an outer class. When it is declared inside a method of the outer class, it is commonly called a method-local inner class in Java. Its scope is limited to the block of a method in which it is declared.

Since a method-local inner class is local to a block, we cannot declare it with access modifiers such as public, protected, or private. We cannot also declare it as static because local classes are associated with a block rather than the enclosing class itself.

The only applicable modifiers for the method-local inner class are final, and abstract. Although strictfp was allowed in older Java versions, it is obsolete and generally not used in modern Java.

Look at the below figure, where we have declared an inner class inside a method m1() of the outer class. The declaration of an inner class inside a method of an outer class is called method local inner class. The applicable modifiers with an inner class inside a method are shown in the below figure.

An example of creating method local inner class in Java with applicable modifiers.

How to Instantiate Method Local Inner Class in Java?


Since a method-local inner class is defined inside the method of an outer class, its scope is limited to that method. Therefore, you can instantiate it only within the same method (or block) in which it is declared. For example:

class Outer {
    void display() {
        class LocalInner {
            void show() {
                System.out.println("Method Local Inner Class");
            }
        }
        LocalInner obj = new LocalInner();
        obj.show();
    }
}

When you call the method in which a local inner class is declared, the class becomes available within that method, and you can create its objects there. You cannot instantiate the local inner class from outside the block where you declare it.

You can access a method-local inner class only within the method, constructor, or initializer block where you declare it. Outside that block, the class goes out of scope, and you cannot access it directly because it is local to that block.

Due to its limited scope, we use a method-local inner class less commonly than member inner classes, static nested classes, and anonymous classes.

Examples of Method-Local Inner Class in Java


Example 1: Instantiate Local Inner Class

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 the outer class.

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 a local inner class"); 
       } 
    } 
 // Create an instance of a method local inner class. 
    InnerClass ic = new InnerClass(); 
 // Call the msg() method using object reference variable ic.
    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 a local inner class

The following program demonstrates how to declare a method-local inner class inside a method and instantiate its object within the same method.

Since the scope of a method-local inner class is limited to the method in which it is declared, we must create its object inside that method. When we call the display() method, the program creates an object of InnerClass and invokes its msg() method.

Why Do We Need a Method-Local Inner Class in Java?


A method-local inner class is a class that we declare inside a method, constructor, or initializer block. We use it when we need a helper class whose functionality is required only within a specific method and not anywhere else in the program.

Java does not support nested methods. In other words, we cannot declare one method inside another method. However, Java allows us to declare a local inner class inside a method and define methods within that class. This feature helps us group related functionality and restrict its scope to the enclosing method.

The main advantages of a method-local inner class are:

  • Improves encapsulation by hiding implementation details.
  • Restricts the visibility of the class to a specific method.
  • Prevents other parts of the program from accessing it.
  • Allows the local class to access the members of the enclosing class and effectively final local variables of the method.

Example 2: Use of Method Local Inner Class

class Test { 
    public void calculate() { 
     // Method-local inner class class 
        Calculator { 
            public void sum(int x, int y) { 
                System.out.println("Sum = " + (x + y)); 
            } 
        } 
        Calculator c = new Calculator(); 
        c.sum(10, 20); 
        c.sum(20, 30); 
        c.sum(30, 40); 
        c.sum(40, 50); 
    } 
    public static void main(String[] args) { 
        Test t = new Test(); 
        t.calculate(); 
    } 
}

Output:

Sum = 30 
Sum = 50 
Sum = 70 
Sum = 90

In this example, the Calculator class is required only inside the calculate() method. Therefore, instead of declaring it as a separate class or a member inner class, we declare it inside the method itself. This keeps the helper class hidden from the rest of the program and limits its scope to the calculate() method.

Thus, we use a method-local inner class when we need a helper class whose functionality is required only within a particular method and should not be accessible outside that method.

Accessing Instance and Static Variables of Outer Class from Method-Local Inner Class


Since an object of a method-local inner class exists within the scope of its enclosing method and is associated with an object of the outer class, it can directly access the instance variables and static variables of the outer class. It can also access its own instance variables and any local variables of the enclosing method that are final or effectively final. Let’s take an example program to understand it.

Example 4: Accessing Variables of Outer Class from Method Local Inner Class

public class AccessingVar 
{ 
// Instance and static variables of outer class. 
   int a = 20; 
   static int b = 30;
// Instance method of the outer class. 
   public void m1()
   { 
      class InnerClass 
      { 
      // Instance variable of inner class. 
         int c = 30; 
         public void printSum(){ 
          // Accessing instance, static, and local variables inside the method local inner class. 
             System.out.println("Sum of three numbers: " +(a+b+c)); 
         } 
     } 
     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

In this example, the method-local inner class InnerClass accesses:

  • The instance variable a of the outer class.
  • The static variable b of the outer class.
  • Its own instance variable c.

Therefore, the printSum() method calculates the sum of all three variables and displays the result. Remember that c is an instance variable (field) of InnerClass, not a local variable. This is because it is declared directly inside the class body. Every object of InnerClass gets its own copy of c. A local variable is declared inside a method, constructor, or block.

Method Local Inner Class Inside Instance Method


If you declare a method-local inner class inside an instance method, the inner class can directly access both instance and static members of the outer class. Let’s take an example of it.

Example 5:

public class OuterClass1 
{  
  // Instance and static variables of the outer class.
   int x = 10;  
   static int y = 20; 
// 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() { 
      // 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(); 
          } 
      } 
   // 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

As you can see in this example, a method-local inner class declared inside an instance method can directly access:

  • Instance members of the outer class (x).
  • Static members of the outer class (y).
  • Static methods of the outer class (m3()).

Method-Local Inner Class Inside Static Method


If you declare a method-local inner class inside a static method, the inner class can directly access only static members of the outer class because the outer class object is not available in the static context.

Example 6:

public class OuterClass2 
{ 
   int x = 10; 
   static int y = 20;
   static void m3()
   { 
      System.out.println("I am in OuterClass1 static method"); 
   } 
   public static void m1() { 
      class InnerClass1 
      { 
          public void m2() { 
            // System.out.println("Value of x: " +x); // Compile-time error 
            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

In this example, we have declared a method-local inner class inside a static method; we cannot directly access instance members (x) because no outer class object is available in the static context. The statement System.out.println(“Value of x: ” + x); produces a compile-time error.

Access Local Variables of Method from a Method-Local Inner Class


A method-local inner class can access local variables and parameters of the enclosing method, provided they are final or effectively final. An effectively final variable is a variable whose value does not change once it is initialized. Let’s understand it with the help of an example program.

Example 7:

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:

20

Since the local variable x is effectively final, the method-local inner class can access it directly.

Prior to Java 8, a local variable had to be declared explicitly as final if a method-local inner class or anonymous inner class accessed it. Since Java 8, the final keyword is optional as long as the variable is effectively final, meaning its value is not modified after initialization.

In addition, a method-local inner class in Java can access all members of its enclosing class that are accessible from the context in which the local class is declared.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.