{"id":22271,"date":"2020-03-07T12:35:29","date_gmt":"2020-03-07T12:35:29","guid":{"rendered":"https:\/\/cnsfly.com\/vytcdc\/?p=22271"},"modified":"2024-09-26T11:44:12","modified_gmt":"2024-09-26T11:44:12","slug":"final-classes-methods-variables-and-arguments-in-java","status":"publish","type":"post","link":"https:\/\/cnsfly.com\/vytcdc\/final-classes-methods-variables-and-arguments-in-java\/","title":{"rendered":"Final Classes, Methods, Variables, and Arguments in Java."},"content":{"rendered":"<p>After working with several Java developers of varying skill levels, I have found that the \u201cfinal\u201d keyword is the most underused concept practiced by them. I have found great reasons to use \u201cfinal\u201d in my coding and I wanted to share some of them.<\/p>\n<p><b>Final Class<\/b><\/p>\n<ul>\n<li>A class is called final class when it is declared with the final keyword.<\/li>\n<li>Final class cannot be inherited.<\/li>\n<\/ul>\n<style>\/*! elementor - v3.23.0 - 23-07-2024 *\/<br \/>\n.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=\".svg\"]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}<\/style>\n<p><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image1.png\" sizes=\"(max-width: 529px) 100vw, 529px\" srcset=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image1.png 529w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image1-300x117.png 300w\" alt=\"\" width=\"529\" height=\"206\" \/><\/p>\n<ul>\n<li>Why do we want to make a class as final?<\/li>\n<li>When you want to do an immutable programming<\/li>\n<li>If you do not want somebody to extend the class, then use the final class.<\/li>\n<li>Whenever somebody extends a class they have the privilege of modifying some of the code inside those class methods, if we do not want to allow it, in certain situations.<\/li>\n<\/ul>\n<p><b>Example<\/b><br \/>\nAll Wrapper Classes like Integer, Float, etc. are final classes. We cannot inherit them.<br \/>\nNobody will be able to extend the above class.<\/p>\n<p><b>Final Methods<\/b><\/p>\n<ul>\n<li>We can declare some or all methods\u00a0final.<\/li>\n<li>Use the final keyword in a method declaration to indicate that the method cannot be overridden by\u00a0subclasses.<\/li>\n<li>The main intention of making a method final would be that the content of the method should not be changed by any outsider.<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image2.png\" alt=\"\" width=\"239\" height=\"112\" \/><\/p>\n<p>Final Variable<\/p>\n<p>The only difference between a normal variable and a final variable is that we can re-assign value to a normal variable but we cannot change the value of a final variable once assigned. Hence, final variables must be used only for the values that we want to remain constant throughout the execution of the program.<\/p>\n<p>Example of final variable<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image3.png\" alt=\"\" width=\"261\" height=\"88\" \/><\/p>\n<ul>\n<li>In the above example, we can change the variable i from 5 to 7.<\/li>\n<li>If we do not want to change the value, we will make variable i as final.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image5.png\" sizes=\"(max-width: 404px) 100vw, 404px\" srcset=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image5.png 404w, https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image5-300x27.png 300w\" alt=\"\" width=\"404\" height=\"37\" \/><\/p>\n<ul>\n<li>Here we have declared variable i as final so changing i value is not allowed.<\/li>\n<\/ul>\n<p><b>Final argument<\/b><\/p>\n<p>Here below, we have not declared argument as final.<\/p>\n<ul>\n<li>In the bellow example doSomethingElse() method declared as final int argument.<\/li>\n<li>So it\u00a0restrict\u00a0\u00a0to change the value of\u00a0doSomethingElse() method argument.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cnsfly.com\/vytcdc\/wp-content\/uploads\/2020\/03\/image7.png\" alt=\"\" width=\"288\" height=\"83\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>After working with several Java developers of varying skill levels, I have found that the \u201cfinal\u201d keyword is the most underused concept practiced by them. I have found great reasons to use \u201cfinal\u201d in my coding and I wanted to share some of them.<\/p>\n","protected":false},"author":1,"featured_media":22820,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[77],"tags":[25,26],"class_list":["post-22271","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-design","tag-development"],"_links":{"self":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/comments?post=22271"}],"version-history":[{"count":1,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22271\/revisions"}],"predecessor-version":[{"id":33865,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/posts\/22271\/revisions\/33865"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/media\/22820"}],"wp:attachment":[{"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/media?parent=22271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/categories?post=22271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cnsfly.com\/vytcdc\/wp-json\/wp\/v2\/tags?post=22271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}