Core JAVA

Local Inner Classes Example in Java

If an inner class has been defined within a code block (typically within the body of a method), then such an inner class is called a local inner class. A local inner class is not a member of the enclosing class and hence it can not have any access specifier. A local inner class will have access to all the members of the enclosing class and it’ll have access to the local final variables in the scope it’s defined.

Program of local inner class:

class LocalInner{
   private String message = "dineshonjava.com";//instance variable
   void display(){
      class Local{
        void msg(){
          System.out.println(message);
        }
      }
     Local loc = new Local();
     loc.msg();
   }
   public static void main(String args[]){
      LocalInner obj = new LocalInner();
      obj.display();
   }
}

output:

After compiling we will get the following class files

  1. LocalInner$1Local.class
  2. LocalInner.class

Internal code generated by the compiler for local inner class:
In such case, compiler creates a class named LocalInner$1Local.class that have the reference of the outer class.

import java.io.PrintStream;
class LocalInner$1Local
{
    final LocalInner this$0;

    LocalInner$1Local()
    {   
        super();
        this$0 = LocalInner.this;
    }
    void msg()
    {
        System.out.println(LocalInner.access$000(LocalInner.this));
    }
}

Rule: Local variable can’t be private, public or protected.

Rules for Local Inner class
1) Local inner class cannot be invoked from outside the method.
2) Local inner class cannot access non-final local variable.
Program of accessing non-final local variable in local inner class

class LocalInner{
   private String message = "dineshonjava.com";//instance variable
   void display(){
     int data = 20;//local variable must be final
      class Local{
        void msg(){
          System.out.println(message+" : "+data);//compile time error
        }
      }
     Local loc = new Local();
     loc.msg();
   }
   public static void main(String args[]){
      LocalInner obj = new LocalInner();
      obj.display();
   }
}

Output: Compile Time Error

Program of accessing final local variable in local inner class:

class LocalInner{
   private String message = "dineshonjava.com";//instance variable
   void display(){
     final int data = 20;//local variable must be final
      class Local{
        void msg(){
          System.out.println(message+" : "+data);
        }
      }
     Local loc = new Local();
     loc.msg();
   }
   public static void main(String args[]){
      LocalInner obj = new LocalInner();
      obj.display();
   }
}

Output:

A local inner class is defined within a method, and the usual scope rules apply to it. It is only accessible within that method, therefore access restrictions (public, protected, package) do not apply. However, because objects (and their methods) created from this class may persist after the method returns, a local inner class may not refer to parameters or non-final local variables of the method.

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago