title: Why can I edit the contents of a final array in Java?
body: <p>The following code in Java uses a <code>final</code> array of <code>String</code>.</p>
  <pre><code>final public class Main {
    public static final String[] CONSTANT_ARRAY = {&quot;I&quot;, &quot;can&quot;, &quot;never&quot;, &quot;change&quot;};
  
    public static void main(String[] args) {
      for (int x = 0; x &lt; CONSTANT_ARRAY.length; x++) {
        System.out.print(CONSTANT_ARRAY[x] + &quot; &quot;);
      }
    }
  }
  </code></pre>
  <p>It displays the following output on the console.</p>
  <blockquote>
  <pre><code>I can never change
  </code></pre>
  </blockquote>
  <p>If we try to reassign the declared <code>final</code> array of type <code>String</code>, we cause an error:</p>
  <pre><code>final public class Main {
    public static final String[] CONSTANT_ARRAY = {&quot;I&quot;, &quot;can&quot;, &quot;never&quot;, &quot;change&quot;};
  
    public static void main(String[] args) {
      CONSTANT_ARRAY={&quot;I&quot;, &quot;can&quot;, &quot;never&quot;, &quot;change&quot;}; //Error - can not assign to final variable CONSTANT_ARRAY.
      for (int x = 0; x &lt; CONSTANT_ARRAY.length; x++) {
        System.out.print(CONSTANT_ARRAY[x] + &quot; &quot;);
      }
    }
  }
  </code></pre>
  <blockquote>
  <p>Error: cannot assign to final variable <code>CONSTANT_ARRAY</code>.</p>
  </blockquote>
  <p>However, the following code works:</p>
  <pre><code>final public class Main {
    public static final String[] CONSTANT_ARRAY = {&quot;I&quot;, &quot;can&quot;, &quot;never&quot;, &quot;change&quot;};
  
    public static void main(String[] args) {
      CONSTANT_ARRAY[2] = &quot;always&quot;;  //Compiles fine.
      for (int x = 0; x &lt; CONSTANT_ARRAY.length; x++) {
        System.out.print(CONSTANT_ARRAY[x] + &quot; &quot;);
      }
    }
  }
  </code></pre>
  <p>It displays</p>
  <blockquote>
  <pre><code>I can always change
  </code></pre>
  </blockquote>
  <p>This mean that we could manage to modify the value of the <code>final</code> array of type <code>String</code>. Can we modify the entire array in this way without violating the immutable rule of <code>final</code>?</p>