{"id":4834,"date":"2014-03-30T15:49:33","date_gmt":"2014-03-30T15:49:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-code-static-final-variable-usage-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:49:33","modified_gmt":"2014-03-30T15:49:33","slug":"java-code-static-final-variable-usage-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-code-static-final-variable-usage-collection-of-common-programming-errors\/","title":{"rendered":"Java Code Static Final variable usage-Collection of common programming errors"},"content":{"rendered":"<p>Setting the reference to <code>final<\/code> ensures you can&#8217;t change the reference. Note however that if the object referred to is <em>mutable<\/em> then you could still change that (not in this instance, since Strings are <code>immutable<\/code>).<\/p>\n<p>I normally make fields <code>final<\/code> and initialise them in the constructor. By favouring immutability my classes are easier to debug and are more likely to be safe in threaded environments. It&#8217;s easier to remove the immutability constraint than add it.<\/p>\n<p>I also do this for method arguments. Rarely (ever) do I want to change a method argument, and making them final will catch inadvertent assignments.<\/p>\n<p>I try <em>not<\/em> to use <code>static<\/code> except for final constants. Unless it&#8217;s something like a logger, I don&#8217;t really want one instance per class, and (of course) this doesn&#8217;t work in the case of multiple classloaders. It&#8217;s also getting close to the singleton anti-pattern, and this impacts on easy testing and (potentially) threading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting the reference to final ensures you can&#8217;t change the reference. Note however that if the object referred to is mutable then you could still change that (not in this instance, since Strings are immutable). I normally make fields final and initialise them in the constructor. By favouring immutability my classes are easier to debug [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4834","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4834"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4834\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}