<div dir="ltr" style="text-align: justify;" trbidi="on">If an <b>inner class</b> has been defined within a <b>code block</b> (typically within the <b>body of a method</b>), then such an inner class is called a <b>local inner class</b>. <b>A local inner class is not a member of the enclosing class and hence it can not have any access specifier</b>. A local inner class will have access to all the members of the enclosing class and it&#8217;ll have access to the local final variables in the scope it&#8217;s defined.</p>
<p> <b>Program of local inner class:</b></p>
<pre class="highlight" name="code">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(); 
 } 
} 
</pre>
<div align="center" id="ads-id"></div>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/inner2.png" /></div>
<p>After compiling we will get the following class files</p>
<ol>
<li><b>LocalInner$1Local.class</b></li>
<li><b>LocalInner.class</b></li>
</ol>
<p><b>Internal code generated by the compiler for local inner class:</b><br />
In such case, compiler creates a class named <b><i>LocalInner$1Local.class</i></b> that have the reference of the outer class.</p>
<pre class="highlight" name="code">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)); 
 } 
} 
</pre>
<p>
<b>Rule: Local variable can&#8217;t be private, public or protected.</b></p>
<p><b>Rules for Local Inner class</b><br />
1) Local inner class cannot be invoked from outside the method.<br />
2) Local inner class cannot access non-final local variable.<br />
<b>Program of accessing non-final local variable in local inner class</b></p>
<pre class="highlight" name="code">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(); 
 } 
} 
</pre>
<p>
<b>Output: Compile Time Error</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="114" src="https://dineshonjava.com/wp-content/uploads/2013/04/inner3.png" width="600" /></div>
<p>
<b>Program of accessing final local variable in local inner class:</b></p>
<pre class="highlight" name="code">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(); 
 } 
} 
</pre>
<p><b>Output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/inner4.png" /></div>
<p>
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.</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/annonymous-inner-class-in-java/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/static-nested-classes-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/annonymous-inner-class-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/static-nested-classes-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '488'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…