<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">The <b><i>static </i></b>is a keyword defined in the java programming language. Keywords are basically reserved words which have specific meaning relevant to a compiler in java programming language likewise the <b><i>static </i></b>keyword indicates the following : </p>
<p></p>
<ul style="text-align: left;">
<li> ;The <b><i>static </i></b>keyword is applicable to an inner class (a class defined within another class), method or field. </li>
</ul>
<ul style="text-align: left;">
<li> ;In java language, a <b><i>static </i></b>keyword is used with a class (inner) needed to be instantiated, even this may be referenced by some other class indicating as if it &#8211; were a top−level class in the class hierarchy.</li>
<li>A static keyword can also be used with a ; field of a class, such a field exists across all the instances of that particular class. ;</li>
</ul>
</div>
<p>The <i><b>static </b></i>keyword is used in java mainly for memory management. We may apply static keyword with variables, methods and blocks. The <b><i>static </i></b>keyword belongs to the class than instance of the class. The <i><b>static</b></i> can be:</p>
<p></p>
<ol style="text-align: left;">
<li>variable (also known as class variable)</li>
<li>method (also known as class method)</li>
<li>block</li>
</ol>
<div align='center' id="ads-id"></div>
<h1 style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: #ff8000; font-family: 'Open Sans'; font-size: 1.883em !important; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 1.2em; margin: 15px 0px 2px; orphans: auto; outline: 0px; padding: 0px 0px 2px; text-align: left; text-indent: 0px; text-transform: none; vertical-align: baseline; white-space: normal; widows: auto; word-spacing: 0px;"><b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 26px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><i style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 26px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">static variable</i></b></h1>
<p></p>
<ul style="text-align: left;">
<li>It is a variable which ;<b style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">belongs to the class</b> ;and ;<b style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">not ;</b>to ;<b style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">object</b>(instance)</li>
<li>Static variables are ;<b style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">initialized only once</b> ;, at the start of the execution . These variables will be initialized first, before the initialization of any instance variables</li>
<li>A ;<b style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">single copy</b> ;to be shared by all instances of the class</li>
<li>A static variable can be ;<b style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">accessed directly ;</b>by the ;<b style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">class name</b> ;and doesn’t need any object</li>
</ul>
<p>Syntax : <;<b style="border: 0px; color: #073763; font-family: 'Open Sans'; font-size: 14px; line-height: 23px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><i style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">class-name>;.<;variable-name>;</i></b></p>
<ul class="list-11" style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; color: black; font-family: 'Open Sans'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 23px; list-style: disc; margin: 5px 0px 5px 10px; orphans: auto; outline: 0px; overflow: hidden; padding: 0px; position: relative; text-align: left; text-indent: 0px; text-transform: none; vertical-align: baseline; white-space: normal; widows: auto; word-spacing: 0px;"></ul>
</div>
<p><b>Advantage of static variable:</b><br />
<b>Understanding problem without static variable-</b></p>
<pre class="highlight" name="code">class Employee{ 
 int empId; 
 String empName; 
 String compName="DAV JavaServices Ltd."; 
} 
</pre>
<p>
Suppose there are 3000 employees in our company, now all instance data members will get memory each time when object is created.All employees have its unique empId and empName so instance data member is good. Here, company refers to the common property of all objects. If we make it static,this field will get memory only once.</p>
<p> <b>Note:</b>static field is shared by all objects.</p>
<p><b>Example of static variable:</b></p>
<pre class="highlight" name="code">//Program of static variable 
 
class Employee{ 
 int empId; 
 String empName; 
 static String compName = "DAV JavaServices Ltd."; 
 
 Employee(int empId, String empName){ 
 this.empId = empId; 
 this.empName= empName; 
 } 
 void display (){ 
 
 System.out.println(empId+" "+empName+" "+compName ); 
 } 
 
 public static void main(String args[]){ 
 Employee employee1 = new Employee(111,"Dinesh"); 
 Employee employee2 = new Employee(222,"Sweety"); 
 
 employee1.display(); 
 employee2.display(); 
 } 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output: </b><br />
111 Dinesh DAV JavaServices Ltd.<br />
222 Sweety DAV JavaServices Ltd.</div>
<p></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/staticvariable.jpg"/></div>
<h1 style="background-color: transparent; border: 0px none; font-family: 'Open Sans'; font-size: 1.883em ! important; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 1.2em; margin: 15px 0px 2px; outline: 0px none; padding: 0px 0px 2px; text-align: left; text-indent: 0px; text-transform: none; vertical-align: baseline; white-space: normal; word-spacing: 0px;"><i style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 26px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 26px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">static method</b></i></h1>
<ul style="text-align: left;">
<li>It is a method which ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">belongs to the class ;</b>and ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">not ;</b>to the ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">object</b>(instance)</li>
<li>A static method ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">can access only static data</b>. It can not access non-static data (instance variables)</li>
<li>A static method ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">can call</b> ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">only</b> ;other ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">static methods ;</b>and can not call a non-static method from it.</li>
<li>A static method can be ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">accessed directly ;</b>by the ;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">class name</b> ;and doesn’t need any object</li>
<li>Syntax : <;<b style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><i style="background-color: transparent; background-position: initial initial; background-repeat: initial initial; border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">class-name>;.<;method-name>;</i></b></li>
<li>A static method cannot refer to “<b><i>this</i></b>” or “<i><b>super</b></i>” keywords in anyway</li>
</ul>
<ul class="list-11" style="background-color: transparent; border: 0px none; font-family: 'Open Sans'; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 23px; list-style: disc outside none; margin: 5px 0px 5px 10px; outline: 0px none; overflow: hidden; padding: 0px; position: relative; text-align: left; text-indent: 0px; text-transform: none; vertical-align: baseline; white-space: normal; word-spacing: 0px;">
<li> </li>
</ul>
<p><b>Example of static method:</b></p>
<pre class="highlight" name="code">//Program of changing the common property of all objects(static field). 
 
class Employee{ 
 int empId; 
 String empName; 
 static String compName = "DAV JavaServices Ltd."; 
 
 static void change(){ 
 compName = "DOJ Ltd"; 
 } 
 
 Student(int empId, String empName){ 
 this.empId = empId; 
 this.empName = empName; 
 } 
 
 void display (){ 
 
 System.out.println(rollno+" "+name+" "+college); 
 } 
 
 public static void main(String args[]){ 
 Employee.change(); 
 
 Employee employee1 = new Employee(111,"Dinesh"); 
 Employee employee2 = new Employee(222,"Sweety"); 
 Employee employee3 = new Employee(333,"Anamika"); 
 
 employee1.display(); 
 employee2.display(); 
 employee3.display(); 
 } 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
111 Dinesh DOJ Ltd.<br />
222 Sweety DOJ Ltd.<br />
333 Anamika DOJ Ltd.</p>
</div>
</div>
<p></p>
<pre class="highlight" name="code">//Program of accessing non-static data member directly from static method main 
 
class A{ 
 int a=40;//non static 
 
 public static void main(String args[]){ 
 System.out.println(a); 
 } 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
Compile Time Error</div>
<p>
<b>Que)why main method is static?</b><br />
<b>Ans)</b> because object is not required to call <i><b>static </b></i>method if it were non-static method, <a href="https://dineshonjava.com/2013/02/java-virtual-machine.html"><b>jvm </b></a>creates object first then call <i><b>main()</b></i> method that will lead the problem of extra memory allocation. </div>
<p>
<b>static block:</b></p>
<p></p>
<ul style="text-align: left;">
<li>Is used to initialize the static data member.</li>
<li>It is executed before main method at the time of classloading.</li>
<li>A ;<b style="background-color: transparent; border: 0px none; font-family: 'Open Sans'; font-size: 14px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 23px; margin: 0px; outline: 0px none; padding: 0px; text-align: left; text-indent: 0px; text-transform: none; vertical-align: baseline; white-space: normal; word-spacing: 0px;">static block helps to initialize the static data members</b>, just like constructors help to initialize instance members</li>
</ul>
<p><b>Example of static block</b></p>
<pre class="highlight" name="code">/Program of static block 
 
class A{ 
 
 static{ 
 System.out.println("static block is invoked"); 
 } 
 
 public static void main(String args[]){ 
 System.out.println("Hello main"); 
 } 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
static block is invoked<br />
Hello main</div>
<p><i><b>Can we execute a program without main() method?</b></i><br />
&#8212; Yes, one of the way is static block but in previous version of JDK not in JDK 1.7. </p>
<pre class="highlight" name="code">//Program of executing a program without main() method 
 
class A{ 
 static{ 
 System.out.println("static block is invoked"); 
 System.exit(0); 
 } 
} 
 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b></p>
<p>static block is invoked (if not JDK7)</p></div>
</div>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/packages-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/final-keyword-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/packages-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/final-keyword-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: '509'});
});
</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…