<p>This post is about Java static keyword along with static methods, static variables, static class, and static blocks with the help of simple examples.</p>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Java static Keyword</strong></span></h3>



<p>In Java, If we want to access class members without creating an instance of the class, we need to declare the class member with <span style="color: #008000;"><strong>static</strong></span> keyword.</p>



<p>In Java, <span style="color: #008000;"><strong>static</strong></span> keyword can be used for the following:</p>



<ul class="wp-block-list"><li><span style="color: #0000ff;"><strong>Blocks</strong></span></li><li><span style="color: #0000ff;"><strong>Variables</strong></span></li><li><span style="color: #0000ff;"><strong>Methods</strong></span></li><li><span style="color: #0000ff;"><strong>Nested classes</strong></span></li></ul>



<p><span style="color: #000000;"><strong>The keyword ;<span style="color: #008000;"><em>static</em></span> ;indicates that the particular member belongs to a class itself, rather than to an instance of that class</strong>.</span></p>



<ul class="wp-block-list"><li>To create a static member (block, variable, method, nested class), precede its declaration with the keyword ;<span style="color: #008000;"><strong>static</strong></span>.</li><li>Static members (method, variables, nested classes) can be accessed directly from other classes using the class name.</li></ul>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Example</strong></span></h5>



<pre class="wp-block-preformatted"><strong>class </strong>Test {
 <strong><span style="color: #008000;"> <em>// static method
 </em></span>static void </strong>m1() {
 System.<strong><em>out</em></strong>.println(<strong>"from m1"</strong>);
 }

 <strong><span style="color: #008000;"><em>// non-static method
 </em></span>void </strong>m2() {
 System.<strong><em>out</em></strong>.println(<strong>"from m2"</strong>);
 }
}

<strong>class </strong>Main {
 <strong>public static void </strong>main(String[] args) {
 Test test = <strong>new </strong>Test();
 <strong><span style="color: #008000;"> <em>//to call non-static method we need object
 </em></span></strong>test.m2();

 <strong><span style="color: #008000;"><em>//calling static method using object
 </em></span></strong>test.<em>m1</em>();

 <strong><span style="color: #008000;"> <em>// calling static method using class name Test
 </em></span></strong>Test.<em>m1</em>();
 }
}</pre>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Output</strong></span></h5>



<pre class="wp-block-preformatted">from m2<br>from m1<br>from m1</pre>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Static Variables</strong></span></h3>



<ul class="wp-block-list"><li>If a variable is ;<span style="color: #0000ff;"><strong>static</strong></span>, it gets memory only once in the class area at the time of class loading and all objects of the class access the same variable.</li><li>Static variables are also known as <strong><span style="color: #008000;">Class variables</span></strong> because they belongs to the class rather than the object of a class.</li><li>Static variables can be created at class-level only.</li></ul>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Example</strong></span></h5>



<pre class="wp-block-preformatted"><strong>class </strong>Employee {
 <strong><span style="color: #008000;">//non-static variables</span></strong>
 <strong>int eid</strong>;
 <strong>int salary</strong>;

 <strong><span style="color: #008000;"><em>//static variable
 </em></span>static </strong>String <em>companyCeo</em>;

 <strong>public void </strong>show() {
 System.<strong><em>out</em></strong>.println(<strong>eid </strong>+ <strong>" : " </strong>+ <strong>salary </strong>+ <strong>" : " </strong>+ <em>companyCeo</em>);
 }
}

public <strong>class </strong>Main {

 <strong>public static void </strong>main(String[] args) {

 Employee e1 = <strong>new </strong>Employee();

 <strong><span style="color: #008000;"><em>//for non-static variable, object is compulsory
 </em></span></strong>e1.<strong>eid </strong>= 100;
 e1.<strong>salary </strong>= 5000;

 <strong><span style="color: #008000;"><em>//for static variable, we do not need object
 </em></span></strong>Employee.<em>companyCeo </em>= <strong>"Arun"</strong>;

 Employee e2 = <strong>new </strong>Employee();
 e2.<strong>eid </strong>= 101;
 e2.<strong>salary </strong>= 8000;

 e1.show();
 e2.show();
 }
}</pre>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Output</strong></span></h5>



<pre class="wp-block-preformatted">100 : 5000 : Arun<br>101 : 8000 : Arun</pre>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>



<ul class="wp-block-list"><li><strong>Employee</strong> class contains two non-static variables <strong>eid</strong> and <strong>salary</strong> (get memory during object creation) and a static variable <strong>companyCeo</strong> (get memory when class loads).</li><li>The instances <strong>e1</strong> and <strong>e2</strong> of <strong>Employee</strong> class contains the separate copies of non-static variables and access the same static variable <strong>companyCeo</strong> with value <strong>&#8220;Arun&#8221;</strong>.</li></ul>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Static Blocks</strong></span></h3>



<ul class="wp-block-list"><li>The <strong><span style="color: #008000;">static block</span></strong> is used to initialize the static variables and is executed only once when the class is loaded in memory. ;</li><li>A class can have multiple static blocks and each static block is executed in the same sequence in which they have been written in a program.</li></ul>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Example</strong></span></h5>



<pre class="wp-block-preformatted"><strong>class </strong>Employee {<br> <strong>int </strong><strong>eid</strong>;<br> <strong>int </strong><strong>salary</strong>;<br><br> <em><strong><span style="color: #008000;">//static variable</span></strong><br></em> <strong>static </strong>String <em>companyCeo</em>;<br><br> <strong><span style="color: #008000;"><em>//execute when class loads<br></em></span></strong><em><strong><span style="color: #008000;"> //first static block</span></strong><br></em> <strong>static </strong>{<br> <em>companyCeo </em>= <strong>"Arun"</strong>;<br> System.<strong><em>out</em></strong>.println(<strong>"Inside static block1"</strong>);<br> }<br><br> <em><strong><span style="color: #008000;">//executes when you create an object</span></strong><br></em> <strong>public </strong>Employee(<strong>int </strong>eid, <strong>int </strong>salary) {<br> <strong>this</strong>.<strong>eid </strong>= eid;<br> <strong>this</strong>.<strong>salary </strong>= salary;<br> System.<strong><em>out</em></strong>.println(<strong>"Inside constructor"</strong>);<br> }<br><br> <em><strong><span style="color: #008000;">//second static block execute after the first static block</span></strong><br></em> <strong>static </strong>{<br> <em>companyCeo </em>= <strong>"Vrun"</strong>;<br> System.<strong><em>out</em></strong>.println(<strong>"Inside static block2"</strong>);<br> }<br><br> <strong>public void </strong>show() {<br> System.<strong><em>out</em></strong>.println(<strong>eid </strong>+ <strong>" : " </strong>+ <strong>salary </strong>+ <strong>" : " </strong>+ <em>companyCeo</em>);<br> }<br>}<br><br><strong>class </strong>Main {<br> <strong>public static void </strong>main(String[] args) {<br><br> Employee e1 = <strong>new </strong>Employee(100, 5000);<br><br> Employee e2 = <strong>new </strong>Employee(101, 6000);<br><br> e1.show();<br> e2.show();<br> }<br>}</pre>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Output</strong></span></h5>



<pre class="wp-block-preformatted">Inside static block1<br>Inside static block2<br>Inside constructor<br>Inside constructor<br>100 : 5000 : Vrun<br>101 : 6000 : Vrun</pre>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>



<p>As soon as the Employee class is loaded,</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<ul class="wp-block-list"><li>The first static block get executed and the value of static variable <strong>companyCeo</strong> set to <span style="color: #008000;"><strong>&#8220;Arun&#8221;</strong></span>.</li><li>Then, the second static block get executed and the value of static variable <strong>companyCeo</strong> overwritten to <strong><span style="color: #008000;">&#8220;Vrun&#8221;.</span></strong></li><li>Now, the constructor of e1 and e2 get executed and both will access the same static variable <strong>companyCeo</strong> with value <span style="color: #008000;"><strong>&#8220;Vrun&#8221;</strong></span>.</li></ul>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Static Methods</strong></span></h3>



<ul class="wp-block-list"><li><span style="color: #000000;">Static methods</span> are also known as <strong><span style="color: #008000;">class methods</span> </strong>because they belongs to the class rather than the object of a class.</li></ul>



<ul class="wp-block-list"><li>Static methods can be accessed directly in static and non-static methods but <strong>we cannot access non-static members inside static method.</strong></li></ul>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Example</strong></span></h5>



<pre class="wp-block-preformatted"><strong>class </strong>Employee {
 <strong><span style="color: #008000;"><em>//non-static variables
 </em></span>int eid</strong>;
 <strong>int salary</strong>;

 <strong><span style="color: #008000;"><em>//static variables
 </em></span>static </strong>String <em>companyCeo</em>;
 <strong>static </strong>String <em>companyName</em>;

 <strong><span style="color: #008000;"><em>//Employee class constructor
 </em></span>public </strong>Employee(<strong>int </strong>eid, <strong>int </strong>salary) {
 <strong>this</strong>.<strong>eid </strong>= eid;
 <strong>this</strong>.<strong>salary </strong>= salary;
 }

 <strong><span style="color: #008000;"><em>//non-static method
 </em></span>void </strong>show() {
 System.<strong><em>out</em></strong>.print(<strong>eid </strong>+ <strong>" : " </strong>+ <strong>salary </strong>+ <strong>" : "</strong>);
 <em><strong><span style="color: #008000;">//access static members inside non-static method</span></strong>
 display</em>();
 }

 <strong><span style="color: #008000;"><em>//static method
 </em></span>static void </strong>display() {
 <strong><span style="color: #008000;"><em>//error: cannot access non-static members inside static method
 <span style="color: #000000;"> //System.out.print(eid + " : " + salary + " : ");
 //show();
 </span></em></span></strong>System.<strong><em>out</em></strong>.print(<em>companyName </em>+ <strong>" : " </strong>+ <em>companyCeo </em>+ <strong>"\n"</strong>);
 }
}

<strong>class </strong>Main {

 <strong>public static void </strong>main(String[] args) {

 Employee e1 = <strong>new </strong>Employee(100, 5000);

 Employee e2 = <strong>new </strong>Employee(101, 6000);

 <strong><em><span style="color: #008000;">//to access static variables and methods, we do not need object.
 //access static variables using class</span> 
 </em></strong>Employee.<em>companyCeo </em>= <strong>"Arun"</strong>;
 Employee.<em>companyName </em>= <strong>"HCL"</strong>;

 <strong><em><span style="color: #008000;">//access static method using class</span> 
 </em></strong>Employee.<em>display</em>();

 e1.show();
 e2.show();
 }
}</pre>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Output</strong></span></h5>



<pre class="wp-block-preformatted">HCL : Arun<br>100 : 5000 : HCL : Arun<br>101 : 6000 : HCL : Arun</pre>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Example explained</strong></span></h5>



<ul class="wp-block-list"><li><strong>Employee</strong> class contains a <strong>non-static</strong> method <span style="color: #008000;"><strong>show()</strong></span> and a <strong>static</strong> method <strong><span style="color: #008000;">display()</span></strong>.</li><li>Static method display() cannot access non-static members and ;<strong>show()</strong> ;is calling static method ;<strong>display()</strong>.</li><li>Inside ;<strong>main()</strong> ;method, we are calling the non-static method show() using the class object and static members using the class name ;<strong>Employee</strong>.</li></ul>



<h3 class="wp-block-heading"><span style="color: #000080;"><strong>Static Nested classes</strong></span></h3>



<p>In Java, we can declare a class inside another class (Outer class). Such classes are known as <span style="color: #0000ff;"><strong>nested classes</strong></span>. Nested classes are of two types:</p>



<ul class="wp-block-list"><li><span style="color: #008000;"><strong>Static Nested Classes</strong></span></li><li><span style="color: #008000;"><strong>Non-static Nested Classes (also known as Inner classes)</strong></span></li></ul>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Differences between static and non-static nested classes:</strong></span></h5>



<ul class="wp-block-list"><li>Nested static class ;can be instantiated without instantiating its outer class (i.e. using Outer class name) but to create instance of Inner class, Outer class instance is necessary.</li><li>A static class can access only the static members of the outer class whereas Inner class can access both static and non-static members of the outer class.</li></ul>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Example</strong></span></h5>



<pre class="wp-block-preformatted"><strong><span style="color: #008000;"><em>//Outer class</em></span>
class </strong>OuterClass {
 <strong>private static </strong>String <em>msg </em>= <strong>"Hello World"</strong>;

 <strong><span style="color: #008000;"> <em>// Static nested class
 </em></span>public static class </strong>NestedStaticClass {

 <strong><span style="color: #008000;"><em>// Only static members of Outer class are accessible</em></span>
 
 public void </strong>show() { <strong><span style="color: #008000;"><em>// non-static method</em></span></strong> 
 System.<strong><em>out</em></strong>.println(<strong>"Message from nested static class: " </strong>+ <em>msg</em>);
 }
 }

 <strong><span style="color: #008000;"><em>// Non-static nested class
 </em></span>public class </strong>InnerClass {

 <strong> <em><span style="color: #008000;">// Both static and non-static members
 // of Outer class are accessible</span> 
 </em>public void </strong>display() { <strong><span style="color: #008000;"><em>// non-static method</em></span></strong>
 System.<strong><em>out</em></strong>.println(<strong>"Message from non-static nested class: " </strong>+ <em>msg</em>);
 }
 }
}

<strong>class </strong>Main {

 <strong>public static void </strong>main(String args[]) {

 <strong><span style="color: #008000;"><em>// Creating instance of nested Static class</em>
 <em>// using Outer class name
 </em></span></strong>OuterClass.NestedStaticClass obj = <strong>new </strong>OuterClass.NestedStaticClass();

 <strong><span style="color: #008000;"><em>// Calling non-static method of nested static class
 </em></span></strong>obj.show();

 <strong><span style="color: #008000;"><em>// To create instance of non-static nested class,
 // outerClass instance is necessary.
 </em></span></strong>OuterClass outer = <strong>new </strong>OuterClass();
 OuterClass.InnerClass inner = outer.<strong>new </strong>InnerClass();

 <span style="color: #008000;"><strong><em>// Calling non-static method of Inner class
 </em></strong></span>inner.display();

 <strong><span style="color: #008000;"><em>// We can also combine above steps in one
 // step to create instance of Inner class
 <span style="color: #000000;">//OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();
 </span></em></span></strong>}
}</pre>



<h5 class="wp-block-heading"><span style="color: #0000ff;"><strong>Output</strong></span></h5>



<pre class="wp-block-preformatted">Message from nested static class: Hello World
Message from non-static nested class: Hello World</pre>


