Use Kotlin Classes

May 22 2024 · Kotlin 1.9.24, Android 14, Kotlin Playground

Lesson 01: Define Classes

Demo

Episode complete

Play next episode

Next
Transcript

You know what classes and reference types are and how to create some in code. Now, it’s time to explore the identity of the class instance created with the same class type.

Define a class that represents a person using a mobile phone. They’re usually recognized by their first and last name, but for simplicity, call them by their first name and use property called name.

class Person (
  var name: String
)

Note: Since this demo focuses on comparing identities, you want to change the person’s name as you go. That’s why you’ll use var, assuming you can modify the property after you create the class. You’ll learn more about it in the next lesson.

Now, you’ll have some fun in the Kotlin Playground. Traditionally, developers use two names when experimenting with user identities: Alice and Bob. Don’t deviate from that tradition. Add the following code into the playground and run it:

class Person (
  var name: String
)

fun main() {
  val alice = Person("Alice")
  val bob = Person("Bob")
  
  println("Alice: " + alice)
  println("Bob: " + bob)
}

Notice the hex number after the class name. This is the actual identifier of the class instance you created, and it’ll be different for every object you create. You can confirm that with a simple check.

Add the following statement inside the main() function:

  println("Alice equal to Bob: " + (alice == bob))

The result is, unsurprisingly, false, which means Alice isn’t Bob. Good.

Next, you’ll create another Bob instance and see if all Bobs are the same.

Keep adding to the main() function:

  val otherBob = Person("Bob")
  println("Other Bob: " + otherBob)
  println("Bob equal to Other Bob: " + (bob == otherBob)) 
  println("Bob's name is equal to Other Bob's name: " + (bob.name == otherBob.name)) 

The first hint that not all Bobs are the same is that the class instance identifier for other Bob is different from that of the original Bob. The following two println() statements confirm that. Both Persons names are Bob, but they’re not the same.

Finally, you’ll have another variable assigned Alice class instance. Note that you don’t create a new one; you just use the reference.

Add this code to the main() function:

val otherAlice = alice
alice.name = "Bob"
println("Other Alice equal to Alice: " + (otherAlice == alice)) 

In this lesson, you’ve gained an understanding of Reference Types, and you’ve applied this knowledge here. Changing the name of the same Person class instance didn’t create a new instance. Therefore, the last println() statement returns true.

There are a few more things you need to confirm:

  • Is Alice actually now using the name Bob?
  • Is this Bob the same as any other Bobs?

Previous experiments suggest that the answers will be Yes and No, or if you speak Kotlin, true and false.

Add this code to the main() function:

  println("Other Alice's name: " + otherAlice.name) 
  println("Other Alice equals to Bob: " + (otherAlice == bob)) 

To summarize: Every time a class instance is created, it gets a new identity. The identity is still different even if instances have precisely the same properties. In future lessons, you’ll learn how to treat instances as equals when properties are the same. But that’s all for now. Thank you for watching!

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion