# Function call in object

**URL:** <https://discuss.kotlinlang.org/t/function-call-in-object/27963>\
**Category:** Support\
**Created:** [January 15, 2024, 2:00pm UTC](https://discuss.kotlinlang.org/t/function-call-in-object/27963 "2024-01-15T14:00:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sievy](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@sievy](https://discuss.kotlinlang.org/u/sievy)\
**Post date:** [January 15, 2024, 2:00pm UTC](https://discuss.kotlinlang.org/t/function-call-in-object/27963/1 "2024-01-15T14:00:12Z")

</div>

I am trying to understand the code snippet below(removed some not relevant bits).  
When ‘status’ is called, why is ‘toString(): String’ function called too? What calls it?

```auto
data class ParkingLot(val size: Int) {
    data class Car(val plate: String, val color: String) {
        override fun toString() = "$plate $color"
    }

    data class Spot(val spotNumber: Int, val isFree: Boolean, val car: Car?) {
        override fun toString() = "$spotNumber $car"
    }

    private val parkingData = Array(size) { Spot(it + 1, true, null) }

    init {
        if (size != 0) println("Created a parking lot with $size spots.")
    }

    override fun toString(): String {
        val str = parkingData.filter { !it.isFree }.joinToString("\n") { it.toString() }
        return when {
            size == 0 -> "Sorry, a parking lot has not been created."
            str.isEmpty() -> "Parking lot is empty."
            else -> str
        }
    }
}

fun main() {
    var parkingLot = ParkingLot(0)
    while (true) {
        val command = readLine()!!.split(" ")
        when (command[0]) {
            "create" -> parkingLot = ParkingLot(command[1].toInt())
            //"park" -> parkingLot.park(ParkingLot.Car(command[1], command[2]))
            //"leave" -> parkingLot.leave(command[1].toInt())
            "status" -> println(parkingLot)
            "exit" -> return
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [January 15, 2024, 4:50pm UTC](https://discuss.kotlinlang.org/t/function-call-in-object/27963/2 "2024-01-15T16:50:36Z")

</div>

`println` internally calls `toString` to turn your object to a string that can be then printed
