<div dir="ltr" style="text-align: justify;" trbidi="on">A nested class that is declared static is called a static nested class. Memory to the objects of any static nested classes are allocated independently of any particular outer class object. A static nested class use the instance variables or methods defined in its enclosing class only through an object reference. A static nested class interacts with the instance members of its outer class or any other class just like a top-level class.</p>
<p> Given below is the syntax of the Static nested class that defines static nested class having keyword static in the outer class.</p>
<pre class="highlight" name="code">class OuterClass { 
 .... 
 static class StaticNestedClass { 
 .... 
 } 
 class InnerClass { 
 .... 
 } 
 } 
</pre>
<p><b>Static nested classes can be accessed by using the enclosing class name:</b></p>
<pre class="highlight" name="code">uterClass.StaticNestedClass 
</pre>
<p><b>If we want to make an object of the static nested class then we have to write down the following code:</b></p>
<pre class="highlight" name="code">OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); 
</pre>
<p>
<i><b>Static nested class facts:</b></i></p>
<ul>
<li>is defined as a static member of the parent class</li>
<li>accepts all accessibility modifiers</li>
<li>it is NOT linked to an outer instance (it can live independently)</li>
<li>has direct access to static members of the parent class regardless of the access modifiers declared in the parent class</li>
<li>has direct access to all members of an instance of the parent class regardless of the access modifiers declared in the parent class</li>
</ul>
<p><b>Program of static nested class that have instance method</b></p>
<pre class="highlight" name="code">class StaticNested{ 
 static String message = "Dinesh on Java"; 
 
 static class Inner{ 
 void display() 
 { 
 System.out.println("message is "+message); 
 } 
 } 
 
 public static void main(String args[]){ 
 StaticNested.Inner obj = new StaticNested.Inner(); 
 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/staticinner.png"/></div>
<p>
In this example, you need to create the instance of static nested class because it has instance method <i><b>display()</b></i>. But you don&#8217;t need to create the object of <i><b>StaticNested </b></i>outer class because nested class is static and static properties, methods or classes can be accessed without object.</p>
<p>There are following class files are generated after the compilation of the program.</p>
<ol>
<li><b>StaticNested$Inner.class</b></li>
<li><b>StaticNested.class </b></li>
</ol>
<p>Internal code generated by the compiler for static nested class</p>
<pre class="highlight" name="code">import java.io.PrintStream; 
 
static class StaticNested$Inner 
{ 
 StaticNested$Inner(){ 
 
 } 
 
 void display(){ 
 System.out.println((new StringBuilder()).append("message is ") 
 .append(StaticNested.message).toString()); 
 } 
 
} 
</pre>
<p>
Program of static nested class that have static method</p>
<pre class="highlight" name="code">class StaticNested{ 
 static String message = "Dinesh on Java"; 
 
 static class Inner{ 
 static void display() 
 { 
 System.out.println("message is "+message); 
 } 
 } 
 
 public static void main(String args[]){ 
 StaticNested.Inner.display();//no need to create the instance of static nested class 
 } 
} 
</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/staticinner.png"/></div>
<p>
The advantage of a static nested class is that it doesn&#8217;t need an object of the containing class to work. This can help you to reduce the number of objects your application creates at runtime.</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/local-inner-classes-example-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/nested-interface-in-java/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/local-inner-classes-example-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/nested-interface-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: '487'});
});
</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…