Categories: Core JAVATutorial

final keyword in Java

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">The <i><b>final <&sol;b><&sol;i>is a keyword&period; This is similar to <b><i>const <&sol;i><&sol;b>keyword in other languages&period; This keyword may not be used as identifiers i&period;e&period; you cannot declare a variable or class with this name in your Java program&period;<&sol;p>&NewLine;<p> The <i><b>final <&sol;b><&sol;i>keyword in java is used to restrict the user&period; The final keyword can be used in many context&period; Final can be&colon;<&sol;p>&NewLine;<p><&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li>variable<&sol;li>&NewLine;<li>method<&sol;li>&NewLine;<li>class<&sol;li>&NewLine;<&sol;ol>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p>&NewLine; The <b><i>final <&sol;i><&sol;b>keyword can be applied with the variables&comma; that have no value it is called blank final variable&period; It can be initialized in the constructor only&period; The blank final variable can be static also which will be initialized in the static block only&period; We will have detailed learning of these&period; Let&&num;8217&semi;s first learn the basics of <b><i>final <&sol;i><&sol;b>keyword&period; <&sol;div>&NewLine;<p>&NewLine; <b>1&period; final variable&colon;<&sol;b> A <i><b>final <&sol;b><&sol;i>variable can only once assigned a value&period; This value cannot be changed latter&period; If <i><b>final <&sol;b><&sol;i>variable used in class then it must assigned a value in class <i><b>constructor<&sol;b><&sol;i>&period; Attempting to change the value of <i><b>final <&sol;b><&sol;i>variable&sol;field will generate error&period; <&sol;p>&NewLine;<p> Unlike the <i><b>constant <&sol;b><&sol;i>value&comma; the value of a final variable is not necessarily known at compile time&period; Final variable comes in mostly two important situations&colon; to prevent accidental changes to method parameters&comma; and with variables accessed by anonymous classes&period;<&sol;p>&NewLine;<p> <b>The syntax of declaring a final type variable is&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public final double radius &equals; 126&period;45&semi;&NewLine;public final int PI &equals; 3&period;145&semi;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Example of final variable&colon;<&sol;b><&sol;p>&NewLine;<p> There is a <i><b>final <&sol;b><&sol;i>variable <b>speedlimit<&sol;b>&comma; we are going to change the value of this variable&comma; but It can&&num;8217&semi;t be changed because <i><b>final <&sol;b><&sol;i>variable once assigned a value can never be changed&period; <&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class Bike&lbrace;&NewLine; final int speedlimit&equals;90&semi;&sol;&sol;final variable&NewLine; &NewLine; void run&lpar;&rpar;&lbrace;&NewLine; speedlimit&equals;400&semi;&NewLine; &rcub;&NewLine;&NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace;&NewLine; Bike obj&equals;new Bike&lpar;&rpar;&semi;&NewLine; obj&period;run&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b><br &sol;>&NewLine;Output&colon;<&sol;b>Compile Time Error<&sol;div>&NewLine;<p>&NewLine;<b>2&period; final method&colon;<&sol;b><br &sol;>&NewLine;A <i><b>final <&sol;b><&sol;i>method cannot be overridden by subclasses and not be hidden&period; This technology prevents unexpected behavior from a subclass for altering a method that may be crucial to the function of the class&period;<&sol;p>&NewLine;<p> <b>Note&colon;<&sol;b> <i><b>private <&sol;b><&sol;i>and <i><b>static <&sol;b><&sol;i>methods are always implicitly <i><b>final <&sol;b><&sol;i>in Java&comma; since they cannot be <b><i>overridden<&sol;i><&sol;b>&period;<&sol;p>&NewLine;<p> <b>The syntax of declaring a final type method is&colon;<&sol;b><&sol;div>&NewLine;<pre class&equals;"highlight" name&equals;"code">public class MyFinalClass &lbrace;&NewLine;&NewLine; public final void myFinalMethod&lpar;&rpar;&NewLine; &lbrace; &NewLine; &sol;&sol;code here&NewLine; &rcub;&NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Example of final method&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class Bike&lbrace;&NewLine; final void run&lpar;&rpar;&NewLine; &lbrace;&NewLine; System&period;out&period;println&lpar;"running"&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine; &NewLine;class Honda extends Bike&lbrace;&NewLine; void run&lpar;&rpar;&NewLine; &lbrace;&NewLine; System&period;out&period;println&lpar;"running safely below 100kmph"&rpar;&semi;&NewLine; &rcub;&NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace;&NewLine; Honda honda &equals; new Honda&lpar;&rpar;&semi;&NewLine; honda&period;run&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b><br &sol;>&NewLine;Output&colon;<&sol;b>Compile Time Error<&sol;div>&NewLine;<p>&NewLine;<b>3&period; final class&colon;<&sol;b><br &sol;>&NewLine;A <i><b>final <&sol;b><&sol;i>class cannot be extended&period; A <b><i>final <&sol;i><&sol;b>class implicitly has all the methods declared as <b><i>final<&sol;i><&sol;b>&comma; but not necessarily the data members&period;<br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>The syntax of declaring a final type method is&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public final class MyFinalClass &lbrace;&NewLine;&NewLine;&sol;&sol;code here&NewLine;&NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Example of final class&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">final class Bike&lbrace;&rcub;&NewLine; &NewLine; class Honda extends Bike&lbrace;&NewLine; void run&lpar;&rpar;&lbrace;&NewLine; System&period;out&period;println&lpar;"running safely below 100kmph"&rpar;&semi;&NewLine; &rcub;&NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace;&NewLine; Honda honda&equals; new Honda&lpar;&rpar;&semi;&NewLine; honda&period;run&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b><br &sol;>&NewLine;Output&colon;<&sol;b>Compile Time Error<&sol;div>&NewLine;<p><b><br &sol;>&NewLine;Is final method inherited&quest;<&sol;b><br &sol;>&NewLine;Yes&comma; <i><b>final <&sol;b><&sol;i>method is inherited but you cannot override it&period;<br &sol;>&NewLine;<b>For Example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class Bike&lbrace;&NewLine; final void run&lpar;&rpar;&lbrace;&NewLine; System&period;out&period;println&lpar;"running&period;&period;&period;"&rpar;&semi;&rcub;&NewLine; &rcub;&NewLine;class Honda extends Bike&lbrace;&NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace;&NewLine; new Honda&lpar;&rpar;&period;run&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b><br &sol;>&NewLine;Output&colon;<&sol;b>running&&num;8230&semi;<&sol;div>&NewLine;<p>&NewLine;<b>What is blank final variable&quest;<&sol;b><br &sol;>&NewLine;A <b><i>final <&sol;i><&sol;b>variable that is not initialized at the time of declaration is known as blank <i><b>final <&sol;b><&sol;i>variable&period; If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed&comma; it is useful&period; For example PAN CARD number of an employee&period; It can be initialized only in constructor&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class Employee&lbrace;&NewLine;int empId&semi;&NewLine;String empName&semi;&NewLine;final String PAN&lowbar;CARD&lowbar;NUMBER&semi;&NewLine;&period;&period;&period;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Can we initialize blank final variable&quest;<&sol;b><br &sol;>&NewLine;Yes&comma; but only in <i><b>constructor<&sol;b><&sol;i>&period;<br &sol;>&NewLine;<b>For example&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class Employee&lbrace;&NewLine;int empId&semi;&NewLine;String empName&semi;&NewLine;final String PAN&lowbar;CARD&lowbar;NUMBER&semi;&NewLine;Employee&lpar;&rpar;&lbrace;&NewLine; PAN&lowbar;CARD&lowbar;NUMBER &equals; "CSA122ASS"&semi;&NewLine; System&period;out&period;println&lpar;PAN&lowbar;CARD&lowbar;NUMBER&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public Static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace;&NewLine; new Employee&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b><br &sol;>&NewLine;Output&colon;<&sol;b>CSA122ASS<&sol;div>&NewLine;<p>&NewLine;<b>static blank final variable<&sol;b><br &sol;>&NewLine;A <b><i><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;04&sol;static-keyword-in-java&period;html" target&equals;"&lowbar;blank">static <&sol;a>final<&sol;i><&sol;b> variable that is not initialized at the time of declaration is known as <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;04&sol;static-keyword-in-java&period;html" target&equals;"&lowbar;blank"><b><i>static <&sol;i><&sol;b><&sol;a>blank <i><b>final <&sol;b><&sol;i>variable&period; It can be initialized only in <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;04&sol;static-keyword-in-java&period;html" target&equals;"&lowbar;blank"><i><b>static <&sol;b><&sol;i><&sol;a>block&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class A&lbrace;&NewLine; static final int data&semi;&sol;&sol;static blank final variable&NewLine; &NewLine; static&lbrace; &NewLine; data&equals;50&semi;&NewLine; &rcub;&NewLine;&NewLine; public Static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace;&NewLine; System&period;out&period;println&lpar;A&period;data&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>What is final parameter&quest;<&sol;b><br &sol;>&NewLine;If you declare any parameter as <i><b>final<&sol;b><&sol;i>&comma; you cannot change the value of it&period;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">class Bike&lbrace;&NewLine; int cube&lpar;final int n&rpar;&lbrace;&NewLine; n&equals;n&plus;2&semi;&sol;&sol;can't be changed as n is final&NewLine; n&ast;n&ast;n&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; public Static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace;&NewLine; Bike b&equals;new Bike&lpar;&rpar;&semi;&NewLine; b&period;cube&lpar;5&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;ff99cc&semi;"><b><br &sol;>&NewLine;Output&colon;<&sol;b>Compile Time Error<&sol;div>&NewLine;<p><&sol;p>&NewLine;<ul>&NewLine;<li>A java variable can be declared using the keyword <b><i>final<&sol;i><&sol;b>&period; Then the <i><b>final <&sol;b><&sol;i>variable can be assigned only once&period;<&sol;li>&NewLine;<li>A variable that is declared as final and not initialized is called a <b><i> blank final variable<&sol;i><&sol;b>&period; A blank final variable forces the constructors to initialize it&period;<&sol;li>&NewLine;<li>Java classes declared as <i><b>final <&sol;b><&sol;i>cannot be extended&period; Restricting inheritance&excl;<&sol;li>&NewLine;<li>Methods declared as <b><i>final <&sol;i><&sol;b>cannot be overridden&period; In methods private is equal to <b><i>final<&sol;i><&sol;b>&comma; but in variables it is not&period;<&sol;li>&NewLine;<li><i><b>final <&sol;b><&sol;i>parameters – values of the parameters cannot be changed after initialization&period; Do a small java exercise to find out the implications of final parameters in method overriding&period;<&sol;li>&NewLine;<li>Java local classes can only reference local variables and parameters that are declared as final&period;<&sol;li>&NewLine;<li>A visible advantage of declaring a java variable as static final is&comma; the compiled java class results in faster performance&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;04&sol;packages-in-java&period;html">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;01&sol;core-java-baby-step-to-be-best-java-ian&period;html">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;04&sol;this-keyword-in-java&period;html">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;static-keyword-in-java&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;this-keyword-in-java&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '508'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago