<div dir="ltr" style="text-align: justify;" trbidi="on">A class declared inside a class is known as nested class. We use nested classes to logically group classes in one place so that it can be more readable and maintainable code. Moreover, it can access all the members of outer class including private members.</p>
<p> <b>Syntax of Nested class</b></p>
<pre class="highlight" name="code">class OuterClass { 
 ... 
 class NestedClass { 
 ... 
 } 
} 
</pre>
<p><b><i>difference between nested class and inner class?</i></b><br />
Nested classes are divided into two categories: <i><b>static </b></i>and <b><i>non-static</i></b>. Nested classes that are declared <i><b>static </b></i>are simply called <i><b>static nested</b></i> classes. Non-static nested classes are called <b><i>inner</i></b> classes. Inner class is a part of nested class. Non-static nested classes are known as inner classes.</p>
<div align='center' id="ads-id"></div>
<p>A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private. (Recall that outer classes can only be declared public or package private.)</p>
<p><i><b>Why Use Nested Classes?</b></i></p>
<p>There are several compelling reasons for using nested classes, among them:</p>
<ol>
<li> It is a way of logically grouping classes that are only used in one place.</li>
<li> It increases encapsulation.</li>
<li> Nested classes can lead to more readable and maintainable code.</li>
</ol>
<ul>
<li> <b>Logical grouping of classes</b>—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such &#8220;helper classes&#8221; makes their package more streamlined.</li>
</ul>
<ul>
<li> <b>Increased encapsulation</b>—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A&#8217;s members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.</li>
</ul>
<ul>
<li> <b>More readable, maintainable code</b>—Nesting small classes within top-level classes places the code closer to where it is used.</li>
</ul>
<p>
<b>Types of Nested class:</b><br />
There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.</p>
<ol>
<li><b><i>non-static nested class(inner class)</i> </b>
<ul>
<li><b>a)Member inner class</b></li>
<li><b>b)Annomynous inner class</b></li>
<li><b>c)Local inner class</b></li>
</ul>
</li>
<li><i><b>static nested class</b></i></li>
</ol>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/typeofnested.jpg" /></div>
<p>
<b>Static Nested Classes</b></p>
<p> As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.</p>
<p> <b>Note:</b> <i>A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.</i><br />
<b>Static nested classes are accessed using the enclosing class name:</b></p>
<pre class="highlight" name="code">OuterClass.StaticNestedClass 
</pre>
<p><b>For example, to create an object for the static nested class, use this syntax:</b></p>
<pre class="highlight" name="code">OuterClass.StaticNestedClass nestedObject = 
new OuterClass.StaticNestedClass(); 
</pre>
<p>
<b>Inner Classes</b></p>
<p> As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object&#8217;s methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.</p>
<p> Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:</p>
<pre class="highlight" name="code">class OuterClass { 
 ... 
 class InnerClass { 
 ... 
 } 
} 
</pre>
<p>An instance of <b>InnerClass </b>can exist only within an instance of <b>OuterClass </b>and has direct access to the methods and fields of its enclosing instance. The next figure illustrates this idea.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/classes-inner.gif" /></div>
<p>
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:</p>
<pre class="highlight" name="code">OuterClass.InnerClass innerObject = outerObject.new InnerClass(); 
</pre>
<p>Additionally, there are two special kinds of inner classes:<b> local classes</b> and <b>anonymous classes</b>.</p>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/different-exception-generate-in-array/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/member-inner-class/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/string-in-switch-java-7-new-concept/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/member-inner-class/">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: '491'});
});
</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…