title: Update a field declared as a val
body: <p>I saw this <code>class</code> definition in an online course that I'm taking:</p>
  
  <pre><code>   class Img(val width: Int, val height: Int, private val data: Array[Int]) {
        def this(w: Int, h: Int) = this(w, h, new Array(w * h))
        def apply(x: Int, y: Int): Int = data(y * width + x)
        def update(x: Int, y: Int, c: Int): Unit = data(y * width + x) = c
      }
  </code></pre>
  
  <p>I'm confused. How can we updated <code>data</code> like this <code>data(y * width + x) = c</code> even though it's declared as <code>val</code>?</p>
  
  <p>Is this a more widely used pattern?</p>