ProgramGuru

Classes, objects and shared references

Classes, objects and shared references

A student record groups a name and mark. A Kotlin class defines properties and operations, and constructing it creates an instance. On the JVM, assigning another variable to that instance shares a reference rather than copying all its data.

What you’ll learn

  • Trace multiple references to one instance.
  • Distinguish data equality and instance identity.

Classes, objects and shared references in the example

  1. One instance

    A student record groups a name and mark.

    One instance
  2. Two references

    Constructor parameters marked val or var become properties.

    Two references
  3. Shared score update

    First and second identify one instance.

    Shared score update
First and second identify one instance. Updating through second is therefore visible as 75 through first. Both bindings are val; the score property remains mutable because it was declared var.

Understand classes, objects and shared references

Constructor parameters marked val or var become properties. A plain class does not automatically compare all its properties for structural equality. Use === for identity, and choose a data class or an explicit equals contract when value comparison is intended.

Follow classes, objects and shared references

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

class Student(val name: String, var score: Int)
fun main() {
  val first = Student("Diya", 70)
  val second = first
  second.score += 5
  println(first.score)
  println(first === second)
}

Expected output

75
true

Follow the execution

First and second identify one instance. Updating through second is therefore visible as 75 through first. Both bindings are val; the score property remains mutable because it was declared var.

Try this in the Kotlin compiler by pasting the complete program.

Practice: Classes, objects and shared references

Your task: How would you create an independent student instance?

Kotlin/JVM · complete program

class Student(val name: String, var score: Int)
fun main() {
  val first = Student("Diya", 70)
  val second = first
  second.score += 5
  println(first.score)
  println(first === second)
}

Worked answer

Construct another Student with the desired values. Assigning first to another name only shares its existing reference.

Classes, objects and shared references: the important boundary

Shared identity is useful when deliberate, but unexpected aliases can spread mutations. A read-only reference does not freeze an object or synchronize its use across threads.

Check your understanding

How would you create an independent student instance?
Choose one answer. You can try again.

Answer and explanation

Construct another Student with the desired values.

Construct another Student with the desired values. Assigning first to another name only shares its existing reference.

Classes, objects and shared references: what to keep in mind

Construct another Student with the desired values. Assigning first to another name only shares its existing reference. Shared identity is useful when deliberate, but unexpected aliases can spread mutations.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Constructors and initialization checks →