title: Kotlin value vs. reference in assignment
body: <p>I would need to have one class reference another. Not create a copy of it but have an actual reference to an already existing instance.</p>
  
  <p>I might be able to explain it better with an example. Were we have a class <code>Image</code>:</p>
  
  <pre><code>class Image(imgPath: String) {
      val height   : Int
      val width    : Int
      val rgbMatrix: MutableList&lt; MutableList&lt; Triple&lt;Int, Int, Int&gt; &gt; &gt;
  
      /* ... etc. ... */
  }
  </code></pre>
  
  <p>Now say I want a class <code>ImageManager</code> that only has a reference to an already existing <code>Image</code>:</p>
  
  <pre><code>class ImageManager(val img: Image) {
     /* ... */
  }
  </code></pre>
  
  <p>So I can have this behaviour: </p>
  
  <pre><code>val image = Image("/res/images/image.png")
  
  val imageManager = ImageManager(image)
  
  // Setting the pixel at (125, 25) to black
  imageManager.img.rgbMatrix[125, 25] = Triple(0, 0, 0)
  
  // Should print the update value(0, 0, 0)
  print( image.rgbMatrix[125, 25] )
  </code></pre>
  
  <p>My questions are: </p>
  
  <ol>
  <li><p>In Kotlin when does in assignment assign a reference and when does it assign a copy?</p></li>
  <li><p>How can I determine what kind of assignment happens when?</p></li>
  <li><p>Is there a part in the official docs where this is detailed? If there is I couldn't find it.</p></li>
  </ol>
  
  <p>Thanks in advance!</p>