{"id":138,"date":"2010-01-02T08:17:17","date_gmt":"2010-01-02T08:17:17","guid":{"rendered":"http:\/\/www.gmarwaha.com\/blog\/?p=138"},"modified":"2014-09-14T05:14:49","modified_gmt":"2014-09-14T05:14:49","slug":"final-modifier-for-method-arguments-what-do-you-think","status":"publish","type":"post","link":"https:\/\/www.gmarwaha.com\/blog\/2010\/01\/02\/final-modifier-for-method-arguments-what-do-you-think\/","title":{"rendered":"Final Modifier for method arguments. What do you think?"},"content":{"rendered":"<p>The IT industry today is sodden with TLAs like SOA, ESB&#8230; and FLAs like AJAX, SOAP and JUNK. i was thinking about refreshing myself with some fundamentals again. Blogging about a basic concept may not be cool, but refreshing &#8211; don&#8217;t you think? I know what you are thinking. You are thinking that i am digressing too much. Ok, lets cut to the chase.<\/p>\n<p>One of the best practices i follow religiously is to use final modifiers for method arguments where applicable. This is <em>&#8220;supposedly&#8221;<\/em> a best-practice written by somebody somewhere. Regardless of whether it is documented as a best-practice or not it is an important concept to understand and use. I have 2 valid reasons to use them for my method arguments. <\/p>\n<p>First, final variables cannot be modified. Come on, everybody knows that. Maybe, but its use is significantly enhanced when it is a method argument and more importantly when you are in a big team environment. <\/p>\n<p>Lets assume that a method takes <em>List<\/em> as its argument. Typically, the intention of that method is to work with the <em>List<\/em> &#8211; add to it, remove elements from it, use its elements in some way, sort it and what not. Consequently when the method returns, the <em>caller<\/em> can investigate the passed List and work with the modifications the <em>callee<\/em> introduced. But the <em>caller<\/em> will be on for a big surprise if the <em>callee<\/em> changes the instance that reference points to itself. <\/p>\n<p>We know that java uses &#8220;Pass by copy of reference&#8221;. If the <em>callee<\/em> points the received reference to a different <em>List<\/em> and then modifies this <em>List<\/em>, the <em>caller<\/em> will not be able to see any change at all. This is because the copy of the reference held by the <em>caller<\/em> still points to the same old <em>List<\/em>. More often than not this is done by mistake and is not intentional. If such a behavior is intentional, final modifier is not required. In all other cases since this leads to bugs in code, it is a good practice to use final modifier for method arguments.<\/p>\n<p>Second, if the method uses the infamous anonymous inner-class syntax to do something, and that inner class wants to use the methods arguments, java requires those arguments to be declared final. This is more of a rule than a valid reason. <\/p>\n<p>Are there more valid reasons? I will be glad to receive information from you guys.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The IT industry today is sodden with TLAs like SOA, ESB&#8230; and FLAs like AJAX, SOAP and JUNK. i was thinking about refreshing myself with some fundamentals again. Blogging about a basic concept may not be cool, but refreshing &#8211; don&#8217;t you think? I know what you are thinking. You are thinking that i am digressing too much. Ok, lets cut to the chase. One of the best practices i follow religiously is to use final modifiers for method arguments where applicable. This is &#8220;supposedly&#8221; a best-practice written by somebody somewhere. Regardless of whether it is documented as a best-practice... <br \/><a class=\"moretag\" href=\"https:\/\/www.gmarwaha.com\/blog\/2010\/01\/02\/final-modifier-for-method-arguments-what-do-you-think\/\">Continue reading...<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,10],"tags":[],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-java","category-serverside"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":9,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":806,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts\/138\/revisions\/806"}],"wp:attachment":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}