<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 <i><b>final </b></i>is a keyword. This is similar to <b><i>const </i></b>keyword in other languages. This keyword may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program.</p>
<p> The <i><b>final </b></i>keyword in java is used to restrict the user. The final keyword can be used in many context. Final can be:</p>
<p></p>
<ol style="text-align: left;">
<li>variable</li>
<li>method</li>
<li>class</li>
</ol>
<div align='center' id="ads-id"></div>
<p>
 The <b><i>final </i></b>keyword can be applied with the variables, that have no value it is called blank final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let&#8217;s first learn the basics of <b><i>final </i></b>keyword. </div>
<p>
 <b>1. final variable:</b> A <i><b>final </b></i>variable can only once assigned a value. This value cannot be changed latter. If <i><b>final </b></i>variable used in class then it must assigned a value in class <i><b>constructor</b></i>. Attempting to change the value of <i><b>final </b></i>variable/field will generate error. </p>
<p> Unlike the <i><b>constant </b></i>value, the value of a final variable is not necessarily known at compile time. Final variable comes in mostly two important situations: to prevent accidental changes to method parameters, and with variables accessed by anonymous classes.</p>
<p> <b>The syntax of declaring a final type variable is:</b></p>
<pre class="highlight" name="code">public final double radius = 126.45;
public final int PI = 3.145;
</pre>
<p>
<b>Example of final variable:</b></p>
<p> There is a <i><b>final </b></i>variable <b>speedlimit</b>, we are going to change the value of this variable, but It can&#8217;t be changed because <i><b>final </b></i>variable once assigned a value can never be changed. </p>
<pre class="highlight" name="code">class Bike{
 final int speedlimit=90;//final variable
 
 void run(){
 speedlimit=400;
 }

 public static void main(String args[]){
 Bike obj=new Bike();
 obj.run();
 }
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b><br />
Output:</b>Compile Time Error</div>
<p>
<b>2. final method:</b><br />
A <i><b>final </b></i>method cannot be overridden by subclasses and not be hidden. This technology prevents unexpected behavior from a subclass for altering a method that may be crucial to the function of the class.</p>
<p> <b>Note:</b> <i><b>private </b></i>and <i><b>static </b></i>methods are always implicitly <i><b>final </b></i>in Java, since they cannot be <b><i>overridden</i></b>.</p>
<p> <b>The syntax of declaring a final type method is:</b></div>
<pre class="highlight" name="code">public class MyFinalClass {

 public final void myFinalMethod()
 { 
 //code here
 }
} 
</pre>
<p>
<b>Example of final method:</b></p>
<pre class="highlight" name="code">class Bike{
 final void run()
 {
 System.out.println("running");
 }
}
 
class Honda extends Bike{
 void run()
 {
 System.out.println("running safely below 100kmph");
 }
 
 public static void main(String args[]){
 Honda honda = new Honda();
 honda.run();
 }
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b><br />
Output:</b>Compile Time Error</div>
<p>
<b>3. final class:</b><br />
A <i><b>final </b></i>class cannot be extended. A <b><i>final </i></b>class implicitly has all the methods declared as <b><i>final</i></b>, but not necessarily the data members.<br />
<b><br />
</b> <b>The syntax of declaring a final type method is:</b></p>
<pre class="highlight" name="code">public final class MyFinalClass {

//code here

} 
</pre>
<p>
<b>Example of final class:</b></p>
<pre class="highlight" name="code">final class Bike{}
 
 class Honda extends Bike{
 void run(){
 System.out.println("running safely below 100kmph");
 }
 
 public static void main(String args[]){
 Honda honda= new Honda();
 honda.run();
 }
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b><br />
Output:</b>Compile Time Error</div>
<p><b><br />
Is final method inherited?</b><br />
Yes, <i><b>final </b></i>method is inherited but you cannot override it.<br />
<b>For Example:</b></p>
<pre class="highlight" name="code">class Bike{
 final void run(){
 System.out.println("running...");}
 }
class Honda extends Bike{
 
 public static void main(String args[]){
 new Honda().run();
 }
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b><br />
Output:</b>running&#8230;</div>
<p>
<b>What is blank final variable?</b><br />
A <b><i>final </i></b>variable that is not initialized at the time of declaration is known as blank <i><b>final </b></i>variable. If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee. It can be initialized only in constructor.</p>
<pre class="highlight" name="code">class Employee{
int empId;
String empName;
final String PAN_CARD_NUMBER;
...
}
</pre>
<p>
<b>Can we initialize blank final variable?</b><br />
Yes, but only in <i><b>constructor</b></i>.<br />
<b>For example:</b></p>
<pre class="highlight" name="code">class Employee{
int empId;
String empName;
final String PAN_CARD_NUMBER;
Employee(){
 PAN_CARD_NUMBER = "CSA122ASS";
 System.out.println(PAN_CARD_NUMBER);
 }

 public Static void main(String args[]){
 new Employee();
 }
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b><br />
Output:</b>CSA122ASS</div>
<p>
<b>static blank final variable</b><br />
A <b><i><a href="https://dineshonjava.com/2013/04/static-keyword-in-java.html" target="_blank">static </a>final</i></b> variable that is not initialized at the time of declaration is known as <a href="https://dineshonjava.com/2013/04/static-keyword-in-java.html" target="_blank"><b><i>static </i></b></a>blank <i><b>final </b></i>variable. It can be initialized only in <a href="https://dineshonjava.com/2013/04/static-keyword-in-java.html" target="_blank"><i><b>static </b></i></a>block.</p>
<pre class="highlight" name="code">class A{
 static final int data;//static blank final variable
 
 static{ 
 data=50;
 }

 public Static void main(String args[]){
 System.out.println(A.data);
 }
}
</pre>
<p>
<b>What is final parameter?</b><br />
If you declare any parameter as <i><b>final</b></i>, you cannot change the value of it.</p>
<pre class="highlight" name="code">class Bike{
 int cube(final int n){
 n=n+2;//can't be changed as n is final
 n*n*n;
 }


 public Static void main(String args[]){
 Bike b=new Bike();
 b.cube(5);
 }
}
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b><br />
Output:</b>Compile Time Error</div>
<p></p>
<ul>
<li>A java variable can be declared using the keyword <b><i>final</i></b>. Then the <i><b>final </b></i>variable can be assigned only once.</li>
<li>A variable that is declared as final and not initialized is called a <b><i> blank final variable</i></b>. A blank final variable forces the constructors to initialize it.</li>
<li>Java classes declared as <i><b>final </b></i>cannot be extended. Restricting inheritance!</li>
<li>Methods declared as <b><i>final </i></b>cannot be overridden. In methods private is equal to <b><i>final</i></b>, but in variables it is not.</li>
<li><i><b>final </b></i>parameters – values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding.</li>
<li>Java local classes can only reference local variables and parameters that are declared as final.</li>
<li>A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.</li>
</ul>
</div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/04/packages-in-java.html">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/04/this-keyword-in-java.html">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/static-keyword-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/this-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: '508'});
});
</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…