Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin secondary constructor

People also ask

How do I create a secondary constructor in Kotlin data class?

Other than that, you can add secondary constructors as explained in Classes and Inheritance - Secondary Constructors. For your class, and example secondary constructor: data class User(val name: String, val age: Int) { constructor(name: String): this(name, -1) { ... } }

What are the two types of constructors in Kotlin?

A Kotlin class can have following two type of constructors: Primary Constructor. Second Constructors.

How many constructors can a class have in Kotlin?

Constructors A class in Kotlin can have a primary constructor and one or more secondary constructors. The primary constructor is a part of the class header, and it goes after the class name and optional type parameters.


Update: Since M11 (0.11.*) Kotlin supports secondary constructors.


For now Kotlin supports only primary constructors (secondary constructors may be supported later).

Most use cases for secondary constructors are solved by one of the techniques below:

Technique 1. (solves your case) Define a factory method next to your class

fun C(s: String) = C(s.length)
class C(a: Int) { ... }

usage:

val c1 = C(1) // constructor
val c2 = C("str") // factory method

Technique 2. (may also be useful) Define default values for parameters

class C(name: String? = null) {...}

usage:

val c1 = C("foo") // parameter passed explicitly
val c2 = C() // default value used

Note that default values work for any function, not only for constructors

Technique 3. (when you need encapsulation) Use a factory method defined in a companion object

Sometimes you want your constructor private and only a factory method available to clients. For now this is only possible with a factory method defined in a companion object:

class C private (s: Int) {
    companion object {
        fun new(s: String) = C(s.length)
    }
}

usage:

val c = C.new("foo")

As the documentation points, you can use a secondary constructor this way

class GoogleMapsRestApiClient constructor(val baseUrl: String) {

    constructor() : this("https://api.whatever.com/")

}

Remember that you must extended the first constructor behavior.


for declaring a secondary constructor Kotlin just use the constructor keyword: like

this is a primary constructor:

class Person constructor(firstName: String) {

}

or

class Person(firstName: String) {

}

for the secondary constructor code like this:

class Person(val name: String) {
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}

it is mandatory to call the primary constructor otherwise, the compiler will throw the following error

Primary constructor call expected

Constructors with init:

class PhoneWatcher : TextWatcher {

    private val editText: EditText
    private val mask: String

    private var variable1: Boolean = false
    private var variable2: Boolean = false

    init {
        variable1 = false
        variable2 = false
    }

    constructor(editText: EditText) : this(editText, "##-###-###-####")

    constructor(editText: EditText, mask: String) {
        this.editText = editText
        this.mask = mask
    }
    ...
}