Warning: include(../include/breadcrumb.php): Failed to open stream: No such file or directory in /home/javabykiran/public_html/thekiranacademy.com/corejava/final-keyword-in-java.php on line 18

Warning: include(): Failed opening '../include/breadcrumb.php' for inclusion (include_path='.:/opt/cpanel/ea-php83/root/usr/share/pear') in /home/javabykiran/public_html/thekiranacademy.com/corejava/final-keyword-in-java.php on line 18

Final Keyword in Java

  • Final keyword can be applied to variable, method and class. final member cannot be changed.
  • A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialize it.
  • Java classes declared as final cannot be extended. Restricting inheritance.
  • Final parameters - values of the parameters cannot be changed after initialization.
  • Final method – can not be overridden.

A visible advantage of declaring a java variable as static final is that the compiled java class results in faster performance. Final is a ‘non-access modifier’ which can only be applied to a variable, method or class. It is basically unchangeable, as in , its value once assigned cannot be changed. The final keyword may be used in various different scenarios.


Example 1: Final variable can not be changed

Example 2: Final method cannot be overridden

Example 3: The final class cannot be extended

Note:

  • Constructor cannot be final
  • Block cannot be final

Some more Examples




Warning: include(../include/google-vertical-ads.php): Failed to open stream: No such file or directory in /home/javabykiran/public_html/thekiranacademy.com/corejava/final-keyword-in-java.php on line 258

Warning: include(): Failed opening '../include/google-vertical-ads.php' for inclusion (include_path='.:/opt/cpanel/ea-php83/root/usr/share/pear') in /home/javabykiran/public_html/thekiranacademy.com/corejava/final-keyword-in-java.php on line 258

Warning: include(../include/internal-page-footer.php): Failed to open stream: No such file or directory in /home/javabykiran/public_html/thekiranacademy.com/corejava/final-keyword-in-java.php on line 299

Warning: include(): Failed opening '../include/internal-page-footer.php' for inclusion (include_path='.:/opt/cpanel/ea-php83/root/usr/share/pear') in /home/javabykiran/public_html/thekiranacademy.com/corejava/final-keyword-in-java.php on line 299