{"componentChunkName":"component---src-templates-post-template-jsx","path":"/history/posts/2020-09-07--001","result":{"data":{"site":{"siteMetadata":{"title":"Blog by Eunyoung","subtitle":"작업 기록 블로그","copyright":"© All rights reserved.","author":{"name":"EunYoung","twitter":"#"},"disqusShortname":"","url":"https://ssongey.github.io"}},"markdownRemark":{"id":"0a150ab4-4701-57fb-9a3b-cf5e21a76b8f","html":"<p>서비스 클래스에서 constant 로 사용하기 위해 아래와 같이 변수를 선언했다</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"> private final List&lt;String&gt; ALPA_LIST = Arrays.asList(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)</code></pre></div>\n<p>접근제어자가 private 이고, 하나의 controller 에만 주입되어 사용하고 있어\nstatic 키워드 없이 사용하였는데,<br>\n해당 변수에 대한 사용의도(private final이 constant로 적합한가?)에 대해 얘기를 하게 되었고\n아래와 같은 의문을 갖게 되었다.</p>\n<h3>1. static vs final</h3>\n<h4>static</h4>\n<p>means there is only <strong>one copy of the variable in memory</strong> shared by all instances of the class.</p>\n<h4>final</h4>\n<p>The final keyword just means the value <strong>can’t be changed.</strong> Without final, any object can change the value of the variable.</p>\n<h3>2. private final vs private final static</h3>\n<p>\n  <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/devHistoryBlog/static/4bf136393090d1956b59790f132c060e/d52e5/1.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n  \n  <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 848px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 67.08333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAABJ0AAASdAHeZh94AAABq0lEQVQ4y4VSi27CMAzs/3/k6PtB0+YJpaXg+ZwG2KRpkU5OHDs5n511XUd9PxBse+yruqGyqqhpWt7XfNdTzbZtuxjXAi3VTUMN24Yt8tZ1pawoCirLUhKKopTLU57T1ylnfxVR1XIG8EGeF1RwDuyJff0wkPeelmWhbLvfad3utAG8v91u8tPO+33fBXfxr3K3bdvL/3g8xD6fzwg+Z9oGUsYTrHGBplmTZgTnaLle5RH55PgISD4wunJMAnyZ40TnvDgulwuFEBjRXq5sX75AnhHjHRljmflOWGCXVjbPM6lpYmZs1URaaxpYE6UUaWPiHWMcFT9ihNWyRHY7l/h7ZTEgsgNAOzFCYmQYWeLux0rafSBzPrB+XAKXbSxgBXjc+SjFp3Y4J+0imTdkbLpRUzPM1E9cqtLUn0cpN7ESLQ/2/0GaorSlMx5i/dphlAehoz2YWmYPoAmvPSr46G5qqDyIxOF8jo1g8dEIJGBQgc8O+2P/F2sZ7KRHGmgMbrTrS7v/sK3voc8wg96HY7aM2JkHW02z/GoPtvDJvY1xqMSKJFZiH8cIfQP9uOZ8hgHE6QAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n    >\n      <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\"\n        alt=\"1\"\n        title=\"\"\n        src=\"/devHistoryBlog/static/4bf136393090d1956b59790f132c060e/d52e5/1.png\"\n        srcset=\"/devHistoryBlog/static/4bf136393090d1956b59790f132c060e/8ff5a/1.png 240w,\n/devHistoryBlog/static/4bf136393090d1956b59790f132c060e/e85cb/1.png 480w,\n/devHistoryBlog/static/4bf136393090d1956b59790f132c060e/d52e5/1.png 848w\"\n        sizes=\"(max-width: 848px) 100vw, 848px\"\n      />\n    </span>\n  </span>\n  \n  </a>\n    </p>\n<hr>\n<p>결론은…<br>\nconstant 로서 사용되는 변수는</p>\n<ul>\n<li>모든 인스턴스에서 동일한 값을 사용하고,  </li>\n<li>새로 메모리를 잡을 필요가 없고</li>\n<li>\n<p>초기화 후에 immutable 한 값이므로\nstatic 키워드를 추가하여 사용한다.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">private final static List&lt;String&gt; ALPA_LIST = Arrays.asList(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)</code></pre></div>\n</li>\n</ul>\n<hr>\n<h4>[ 200914 추가내용 ]</h4>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Arrays.asList()\n# remove(), add()를 지원하지 않음. 사용시 java.lang.UnsupportedOperationException 발생\n# ArrayList와 마찬가지로 set(), get(), contains() 제공</code></pre></div>\n<p>즉 value가 변할 수 있는 객체이므로, 상수로서 적합하지 않다.\n따라서 immutable 하도록 아래와 같이 사용한다.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\"> private final static List&lt;String&gt; ALPA_LIST = Collections.unmodifiableList(Arrays.asList(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;))</code></pre></div>\n<p>Java 9부터는 List <E> .of (E… elements) 정적 팩토리 메서드를 사용</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">@Test(expected = UnsupportedOperationException.class)\npublic final void givenUsingTheJava9_whenUnmodifiableListIsCreated_thenNotModifiable() {\n    final List&lt;String&gt; list = new ArrayList&lt;&gt;(Arrays.asList(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;));\n    final List&lt;String&gt; unmodifiableList = List.of(list.toArray(new String[]{}));\n    unmodifiableList.add(&quot;four&quot;);</code></pre></div>\n<h4>참고</h4>\n<ul>\n<li><a href=\"https://djkeh.github.io/articles/Why-should-final-member-variables-be-conventionally-static-in-Java-kor/\">https://djkeh.github.io/articles/Why-should-final-member-variables-be-conventionally-static-in-Java-kor/</a></li>\n<li><a href=\"https://stackoverflow.com/questions/1415955/private-final-static-attribute-vs-private-final-attribute\">https://stackoverflow.com/questions/1415955/private-final-static-attribute-vs-private-final-attribute</a></li>\n<li><a href=\"https://stackoverflow.com/questions/13772827/difference-between-static-and-final\">https://stackoverflow.com/questions/13772827/difference-between-static-and-final</a></li>\n</ul>","fields":{"tagSlugs":["/tags/java/","/tags/constant/"],"slug":"/history/posts/2020-09-07--001"},"frontmatter":{"title":"[Java] Final vs Static","tags":["java","constant"],"date":"2020-09-07","description":""}}},"pageContext":{"slug":"/history/posts/2020-09-07--001"}},"staticQueryHashes":[]}