Constructor overloading with Kotlin

You can define extra constructors in the class body

data class User(val firstName: String, val lastName: String) {

    constructor(firstName: String) : this(firstName, "")

}

These ‘secondary constructors’ have to call through to the primary constructor or a different secondary constructor. See the Official documentation on constructors.

So, in effect this is the same as just a primary constructor with default argument, which would be the idiomatic way to go.

data class User(val firstName: String, val lastName: String = "")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)