{"id":4918,"date":"2014-03-30T16:55:50","date_gmt":"2014-03-30T16:55:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/setting-a-final-class-attribute-collection-of-common-programming-errors\/"},"modified":"2014-03-30T16:55:50","modified_gmt":"2014-03-30T16:55:50","slug":"setting-a-final-class-attribute-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/setting-a-final-class-attribute-collection-of-common-programming-errors\/","title":{"rendered":"Setting a final class attribute-Collection of common programming errors"},"content":{"rendered":"<p>It&#8217;s not allowed because you <strong><em>could<\/em><\/strong> call <code>setA()<\/code> via some other non-constructor method later on which would violate the final protection. Since final is a <em>compile time<\/em> enforced operation, the compiler enforces final by forcing initialization to occur in constructors or in-line.<\/p>\n<p>In your simple example, all looks good but if you later updated your class to something like the following, the problem become more obvious&#8230;<\/p>\n<pre><code>public class FinalTest {\n  private final Object a;\n  \/\/Constructor\n  public FinalTest() {\n    setA();\n  }\n\n  private void setA() {\n    a = new Object;\n  }\n\n  public void doSomething() {\n   this.setA(); \/\/ not good because a is final\n  }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not allowed because you could call setA() via some other non-constructor method later on which would violate the final protection. Since final is a compile time enforced operation, the compiler enforces final by forcing initialization to occur in constructors or in-line. In your simple example, all looks good but if you later updated your [&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-4918","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4918","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=4918"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4918\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}