<div dir="ltr" style="text-align: justify;" trbidi="on">A class that is declared inside a class but outside a method is known as member inner class.<br />
<b>Invocation of Member Inner class</b></p>
<ol>
<li>From within the class</li>
<li>From outside the class</li>
</ol>
<p><b>Example of member inner class that is invoked inside a class</b><br />
In this example, we are invoking the method of member inner class from the display method of Outer class.</p>
<pre class="highlight" name="code">class Outer{ 
 private int value = 70; 
 class Inner{ 
 void show(){ 
 System.out.println("value is "+value ); 
 } 
 } 
 
 void display(){ 
 Inner in = new Inner(); 
 in.show(); 
 } 
 public static void main(String args[]){ 
 Outer obj = new Outer(); 
 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/inner.png"/></div>
<p>
See in the directory there are the two following class files after compiling.<br />
<b><i>Outer.class</i></b><br />
<b><i>Outer$Inner.class</i></b></p>
<p><b>Internal code generated by the compiler for member inner class:</b><br />
The java compiler creates a class file named <i><b>Outer$Inner</b></i> in this case. The Member inner class have the reference of <i><b>Outer </b></i>class that is why it can access all the data members of Outer class including private.</p>
<pre class="highlight" name="code">import java.io.PrintStream; 
 
class Outer$Inner 
{ 
 final Outer this$0; 
 Outer$Inner() 
 { super(); 
 this$0 = Outer.this; 
 } 
 
 void show() 
 { 
 System.out.println((new StringBuilder()).append("value is ") 
 .append(Outer.access$000(Outer.this)).toString()); 
 } 
 } 
</pre>
<p><b>Example of member inner class that is invoked outside a class</b></p>
<p> In this example, we are invoking the <i><b>show()</b></i> method of Inner class from outside the outer class i.e. <i><b>Test </b></i>class.</p>
<p> <i><b>//Program of member inner class that is invoked outside a class</b></i></p>
<pre class="highlight" name="code">class Outer{ 
 private int value = 70; 
 class Inner{ 
 void show(){System.out.println("value is "+value);} 
 } 
} 
 
class Test{ 
 public static void main(String args[]){ 
 Outer obj = new Outer(); 
 Outer.Inner in = obj.new Inner(); 
 in.show(); 
 } 
} 
</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/inner-1.png"/></div>
<p>
<b>Rules for Inner Classes:</b><br />
Following properties can be noted about Inner classes:</p>
<ul>
<li>The outer class (the class containing the inner class) can instantiate as many number of inner class objects as it wishes, inside it’s code.</li>
<li>If the inner class is public &; the containing class as well, then code in some other unrelated class can as well create an instance of the inner class.</li>
<li>In above case the inner class can be created as follows:</li>
</ul>
<pre class="highlight" name="code"><;OuterClassName>; outerObj = new <;OuterClassName>;(arguments); 
outerObj.<;InnerClassName>; innerObj = outerObj.new <;InnerClassName>;(arguments); 
</pre>
<ul>
<li> No inner class objects are automatically instantiated with an outer class object.</li>
<li> If the inner class is static, then static inner class can be instantiated without an outer class instance, otherwise, the inner class object must be associated with an instance of the outer class.</li>
<li>Inner class code has free access to all elements of the outer class object that contains it, by name (no matter what the access level of the elements is), if the inner class has a varible with same name then the outer class’s variable can be access like this:</li>
</ul>
<pre class="highlight" name="code"><;OuterClassName>;.this.<;variableName>; 
</pre>
<ul>
<li> The outer class can call even the private methods of the inner class</li>
</ul>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/inner-nested-classes-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/annonymous-inner-class-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/inner-nested-classes-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/annonymous-inner-class-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: '490'});
});
</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…