{"id":4987,"date":"2014-03-30T17:31:10","date_gmt":"2014-03-30T17:31:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/add-or-remove-objects-to-a-final-static-hashset-at-runtime-in-java-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:31:10","modified_gmt":"2014-03-30T17:31:10","slug":"add-or-remove-objects-to-a-final-static-hashset-at-runtime-in-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/add-or-remove-objects-to-a-final-static-hashset-at-runtime-in-java-collection-of-common-programming-errors\/","title":{"rendered":"Add (or remove) objects to a final static HashSet at runtime in Java?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m working with Java 4; some time ago I came across a variable which was declared in a public class as:<\/p>\n<pre><code>final private static Set name = new HashSet(){\n  {\n     add(object1); \n     ...; \n     add(objectN);\n  }\n};\n<\/code><\/pre>\n<p>and I needed to add (or remove) objects to it at runtime under some circumstances.<\/p>\n<p>The class had a public constructor which was called before I had to either add or remove objects to said Set.<\/p>\n<p>I thought that final variables were treated as constants so I wouldn&#8217;t be able to call the .add(object) and .remove(object) methods on it at runtime. But I did it anyway, I created two public methods to perform the add and remove operations, and it worked.<\/p>\n<p>Why? I&#8217;d expected it either not to compile or to throw some kind of exception at runtime.<\/p>\n<p>Can someone explain?<\/p>\n<p>Thank you very much,<\/p>\n<p>best regards<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m working with Java 4; some time ago I came across a variable which was declared in a public class as: final private static Set name = new HashSet(){ { add(object1); &#8230;; add(objectN); } }; and I needed to add (or remove) objects to it at runtime under some circumstances. The class had a public [&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-4987","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4987","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=4987"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4987\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}