{"id":215,"date":"2017-03-18T01:02:17","date_gmt":"2017-03-17T19:32:17","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=215"},"modified":"2021-01-12T14:46:25","modified_gmt":"2021-01-12T09:16:25","slug":"final-keyword-java-example","status":"publish","type":"post","link":"https:\/\/java2blog.com\/final-keyword-java-example\/","title":{"rendered":"Final keyword in java with example"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">\n<p>In this post, we will see about final keyword in <a href=\"https:\/\/java2blog.com\/core-java-tutorial-for-beginners-experienced\/\">java<\/a>. Final keyword can be associated with:<\/p>\n<ul style=\"text-align: left;\">\n<li>variable<\/li>\n<li>method<\/li>\n<li><a href=\"https:\/\/java2blog.com\/object-class-java\/\">class<\/a><\/li>\n<\/ul>\n<p>Final is often used when you want restrict others from doing any changes.<\/p>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Final_variable\">Final variable<\/a><ul><li><a href=\"#Blank_final_variable\">Blank final variable<\/a><\/li><li><a href=\"#static_blank_final_variable\">static blank final variable<\/a><\/li><\/ul><\/li><li><a href=\"#Final_method\">Final method<\/a><\/li><li><a href=\"#Final_class\">Final class<\/a><\/li><li><a href=\"#Summary_for_final_keyword\">Summary for final keyword<\/a><\/li><\/ul><\/div>\n\n<p><strong>Let&#8217;s go through each by one.<\/strong><\/p>\n<hr\/>\n<h2><span id=\"Final_variable\"><span style=\"color: #f89820;\">Final variable<\/span><\/span><\/h2>\n<div style=\"text-align: left;\">If you make any <a href=\"https:\/\/java2blog.com\/variables-java\/\" target=\"_blank\" rel=\"noopener noreferrer\">variable<\/a> final then you are not allowed to change its value later.It will be constant.If you try to change value, then compiler will give you error.<\/div>\n<div style=\"text-align: left;\"><\/div>\n<div style=\"text-align: left;\">Let&#8217;s take a simple example:<\/div>\n<\/div>\n<pre name=\"code\" mark=\"10\">package org.arpit.java2blog;\n\npublic class FinalExample{\n\n    final int count=10;\n\n    public void setCount()\n    {\n        count=20;\n    }\n}\n<\/pre>\n<div style=\"text-align: left;\">In above example, you will get compilation error with text &#8220;The final field FinalExampleMain.count cannot be assigned&#8221; at highlighted row.<\/div>\n<div style=\"text-align: left;\"><\/div>\n<h3 style=\"text-align: left;\"><span id=\"Blank_final_variable\">Blank final variable<\/span><\/h3>\n<div style=\"text-align: left;\">Blank final variable is the variable which is not initialised at the time of declaration. It can be initialised only in constructor.<\/div>\n<div dir=\"ltr\" style=\"text-align: left;\">But if you do not initialise final variable, you will get compilation error as below.<\/div>\n<pre name=\"code\" mark=\"6\">package org.arpit.java2blog;\n\npublic class FinalExample {\n\n    final int count;\n\n}\n\n<\/pre>\n<div dir=\"ltr\" style=\"text-align: left;\">In above example, you will get compilation error with text &#8220;The blank final field count may not have been initialised&#8221; at highlighted row.<br \/>\nYou can initialize final variable once in a constructor as below.<\/p>\n<pre name=\"code\" class=\"\">package org.arpit.java2blog;\n\npublic class FinalExample {\n\n    final int count;\n\n    FinalExample(int count)\n    {\n        this.count=count;\n    }   \n        public static void main(String args[])\n    {\n        FinalExample fe=new FinalExample(10);\n        System.out.println(fe.count);   \n    }\n}\n<\/pre>\n<p>Above code will work fine. You might be thinking what may be use of it.<br \/>\nLet&#8217;s say you have Employee class and it has an attribute called empNo. Once object is created, you don&#8217;t want to change empNo.<br \/>\nSo you can declare it final and initialize it in constructor.<\/p>\n<\/div>\n<h3><span id=\"static_blank_final_variable\">static blank final variable<\/span><\/h3>\n<p>static\u00a0blank final variable is <a href=\"https:\/\/java2blog.com\/static-keyword-in-java\/\" rel=\"noopener noreferrer\" target=\"_blank\">static<\/a> variable which is not initialized at the time of declaration. It can be initialized only in static block.<\/p>\n<pre name=\"code\">package org.arpit.java2blog;\n\npublic class FinalExample {\n\n    static final int count;\n\n    static   \n    {\n             count=10; \n    }   \n    public static void main(String args[])\n    {\n         System.out.println(FinalExample.count);    \n    }\n}\n<\/pre>\n<p>When you run above program, you will get below output:<\/p>\n<div class=\"content-box-green\">10<\/div>\n<hr\/>\n<h2><span id=\"Final_method\"><span style=\"color: #f89820;\">Final method<\/span><\/span><\/h2>\n<p>You can not override final methods in subclasses. You can call parent&#8217;s class final method using subclass&#8217;s object but you can not override it.<\/p>\n<pre name=\"code\" class=\"\" mark=\"12\">package org.arpit.java2blog;\n\npublic class Shape{\n\n    public final void draw()\n    {\n        System.out.println(\"Draw method in shape class\");\n    }\n}\n\nclass Rectangle extends Shape\n{\n    public void draw()\n    {\n        System.out.println(\"Draw method in shape class\");\n    }\n\n    public static void main(String args[])\n    {\n        Rectangle rectangle= new Rectangle();\n        rectangle.draw();\n    }\n}\n<\/pre>\n<p>You will get compilation error at highlighted row with text <strong>&quot;Cannot override the final method from Shape&quot;<\/strong>.<\/p>\n<p>If you remove draw method from rectangle class, it will work fine.<\/p>\n<pre name=\"code\" class=\"\">package org.arpit.java2blog;\n\npublic class Shape{\n\n    public final void draw()\n    {\n        System.out.println(\"Draw method in shape class\");\n    }\n}\n\nclass Rectangle extends Shape\n{\n    public static void main(String args[])\n    {\n        Rectangle rectangle= new Rectangle();\n        rectangle.draw();\n    }\n}\n<\/pre>\n<p>When you run above program, you will get below output:<\/p>\n<div class=\"content-box-green\">Draw method in shape class<\/div>\n<hr\/>\n<h2><span id=\"Final_class\"><span style=\"color: #f89820;\">Final class<\/span><\/span><\/h2>\n<p>If you declare a class final, no other class can extend it.<\/p>\n<pre name=\"code\" class=\"\" mark=\"12\">package org.arpit.java2blog;\n\nfinal class Shape{\n\n    public final void draw()\n    {\n        System.out.println(\"Draw method in shape class\");\n    }\n}\n\nclass Rectangle extends Shape\n{   \n    public static void main(String args[])\n    {\n        Rectangle rectangle= new Rectangle();\n        rectangle.draw();\n    }\n}\n<\/pre>\n<p>You will get compilation error at highlighted row with text &quot;The type Rectangle cannot subclass the final class Shape&quot;.<\/p>\n<hr\/>\n<h2><span id=\"Summary_for_final_keyword\"><span style=\"color: #f89820;\">Summary for final keyword<\/span><\/span><\/h2>\n<\/p>\n<ol>\n<li>You can use final keyword with variable, method, and class.<\/li>\n<li>You can not use final keyword with constructor.<\/li>\n<li>If you do not initialize final variable(unless assigned in constructor), you will get compilation error.<\/li>\n<li>You can not change value of final variable once initialized.<\/li>\n<li>You can not override final methods in the sub class.<\/li>\n<li>You can not extend any final class.<\/li>\n<\/ol>\n<p>You may also like:<br \/>\n<a href=\"http:\/\/www.java2blog.com\/2013\/12\/static-keyword-in-java.html\">Static keyword in java with examples<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsFinal variableBlank final variablestatic blank final variableFinal methodFinal classSummary for final keyword In this post, we will see about final keyword in java. Final keyword can be associated with: variable method class Final is often used when you want restrict others from doing any changes. Let&#8217;s go through each by one. Final variable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[180],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/215"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}