{"id":1196,"date":"2022-02-25T16:40:14","date_gmt":"2022-02-25T16:40:14","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1196"},"modified":"2022-02-25T16:40:14","modified_gmt":"2022-02-25T16:40:14","slug":"java-final-keyword","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-final-keyword\/","title":{"rendered":"Java final keyword"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java final variables, methods and classes with examples.<\/p>\n\n\n\n<p id=\"introduction\">In Java, the&nbsp;<code>final<\/code>&nbsp;keyword is used to denote constants. It can be used with variables, methods, and classes.<\/p>\n\n\n\n<p>Once any entity (variable, method or class) is declared&nbsp;<code>final<\/code>, it can be assigned only once. That is,<\/p>\n\n\n\n<ul><li>the final variable cannot be reinitialized with another value<\/li><li>the final method cannot be overridden<\/li><li>the final class cannot be extended<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"final-variable\">1. Java final Variable<\/h2>\n\n\n\n<p>In Java, we cannot change the value of a final variable. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create a final variable\n    final int AGE = 32;\n\n    \/\/ try to change the final variable\n    AGE = 45;\n    System.out.println(\"Age: \" + AGE);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>In the above program, we have created a final variable named&nbsp;<var>age<\/var>. And we have tried to change the value of the final variable.<\/p>\n\n\n\n<p>When we run the program, we will get a compilation error with the following message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cannot assign a value to final variable AGE\n    AGE = 45;\n    ^\n<\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: It is recommended to use uppercase to declare final variables in Java.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"final-method\">2. Java final Method<\/h2>\n\n\n\n<p>Before you learn about final methods and final classes, make sure you know about the\u00a0Java Inheritance.<\/p>\n\n\n\n<p>In Java, the&nbsp;<code>final<\/code>&nbsp;method cannot be overridden by the child class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class FinalDemo {\n    \/\/ create a final method\n    public final void display() {\n      System.out.println(\"This is a final method.\");\n    }\n}\n\nclass Main extends FinalDemo {\n  \/\/ try to override final method\n  public final void display() {\n    System.out.println(\"The final method is overridden.\");\n  }\n\n  public static void main(String&#91;] args) {\n    Main obj = new Main();\n    obj.display();\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have created a final method named&nbsp;<code>display()<\/code>&nbsp;inside the&nbsp;<code>FinalDemo<\/code>&nbsp;class. Here, the&nbsp;<var>Main<\/var>&nbsp;class inherits the&nbsp;<var>FinalDemo<\/var>&nbsp;class.<\/p>\n\n\n\n<p>We have tried to override the final method in the&nbsp;<var>Main<\/var>&nbsp;class. When we run the program, we will get a compilation error with the following message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> display() in Main cannot override display() in FinalDemo\n  public final void display() {\n                    ^\n  overridden method is final\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"final-class\">3. Java final Class<\/h2>\n\n\n\n<p>In Java, the final class cannot be inherited by another class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create a final class\nfinal class FinalClass {\n  public void display() {\n    System.out.println(\"This is a final method.\");\n  }\n}\n\n\/\/ try to extend the final class\nclass Main extends FinalClass {\n  public  void display() {\n    System.out.println(\"The final method is overridden.\");\n  }\n\n  public static void main(String&#91;] args) {\n    Main obj = new Main();\n    obj.display();\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have created a final class named&nbsp;<var>FinalClass<\/var>. Here, we have tried to inherit the final class by the&nbsp;<var>Main<\/var>&nbsp;class.<\/p>\n\n\n\n<p>When we run the program, we will get a compilation error with the following message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cannot inherit from final FinalClass\nclass Main extends FinalClass {\n                   ^<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java final variables, methods and classes with examples. In Java, the&nbsp;final&nbsp;keyword is used to denote constants. It can be used with variables, methods, and classes. Once any entity (variable, method or class) is declared&nbsp;final, it can be assigned only once. That is, the final variable cannot be reinitialized [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[484],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1196"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1196"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1196\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}