Kotlin secondary constructor

SyntaxConstructorKotlin

Syntax Problem Overview


How do I declare a secondary constructor in Kotlin?

Is there any documentation about that?

Following does not compile...

class C(a : Int) {
  // Secondary constructor
  this(s : String) : this(s.length) { ... }
}

Syntax Solutions


Solution 1 - Syntax

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")

Solution 2 - Syntax

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.

Solution 3 - Syntax

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

Solution 4 - Syntax

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
    }
    ...
}

Solution 5 - Syntax

Too late to answer, but here is my humble contribution:)

As Kotlin supports default param value, (note: I want to use power of null) like this:

data class MyClass(val a: Int? = null, val b: String? = null, val c: Double? = null)

we dont need to have multiple constructor. but even if we want it, we can do it this way as well:

data class MyClass(val a: Int?, val b: String?, val c: Double?){
    constructor() : this(null,null,null)
    constructor(a : Int) : this(a,null,null)
    constructor(a : Int, b: String) : this(a,b,null)
}

we can instantiate this class in following ways:

println(MyClass().toString())
println(MyClass(1).toString())
println(MyClass(1,"String").toString())
println(MyClass(1,"String",0.5).toString())

and lets see the result as well:

enter image description here

Solution 6 - Syntax

Custom View Example with Multiple Constructors in Android:

class ShaderBackground : View {

 
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    private fun init() {

       // Init stuff here
        paint = Paint();
        paint.strokeWidth = 10f;
        paint.style = Paint.Style.FILL_AND_STROKE;
   
    }

Solution 7 - Syntax

You can define multiple constructors in Kotlin with constructor but you need to skip default constructor class AuthLog(_data: String)

class AuthLog {

    constructor(_data: String): this(_data, -1)

    constructor(_numberOfData: Int): this("From count ", _numberOfData)

    private constructor(_data: String, _numberOfData: Int)

}

For more details see here

Update

Now you can define default constructor

class AuthLog(_data: String, _numberOfData: Int) {

    constructor(_data: String): this(_data, -1) {
        //TODO: Add some code here if you want
    }

    constructor(_numberOfData: Int): this("From count", _numberOfData)

}

Solution 8 - Syntax

I just saw this question and I think there may be another technique which sounds even better than those proposed by Andrey.

class C(a: Int) {
    class object {
        fun invoke(name: String) = C(name.length)
    }        
}

That you can either write something like val c:C = C(3) or val c:C = C("abc"), because the invoke methods work kind of the same way the apply methods work in Scala.

Update

As of now, secondary constructors are already part of the language spec so this workaround shouldn't be used.

Solution 9 - Syntax

The code snippet below should work

class  C(a:Int){
  constructor(s:String):this(s.length){..}
}

Solution 10 - Syntax

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

you can try this.

Solution 11 - Syntax

kotlin Secondary constructor example

class Person(name: String){
    var name=""
    var age=0

    constructor(age :Int,name : String)  : this(name){
        this.age=age
        this.name=name
    }
    fun display(){
        print("Kotlin Secondary constructor $name  , $age")
    }
}

main function

fun main(args : Array<String>){

    var objd=Person(25,"Deven")
    objd.display()
}

Solution 12 - Syntax

I was a bit confused with most of the answers. To make it easy to understand I am adding an example with more elements :

   @JsonInclude(JsonInclude.Include.NON_NULL)
   data class Response(val code: String) {
      var description: String? = null
      var value: String? = null

      constructor(code: String, description: String?) : this(code) {
          this.description = description
      }

      constructor(code: String, description: String?, value: String) : this(code, description) {
          this.value = value
      }
   }

Solution 13 - Syntax

Use the variable 'internal' and then you can add multiple constructors inside single class like below. This will work flawlessly.

class AuthModel {
var code: String? = null

internal constructor(code: String?) {
    this.code = code
}

internal constructor() {}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionironicView Question on Stackoverflow
Solution 1 - SyntaxAndrey BreslavView Answer on Stackoverflow
Solution 2 - SyntaxcesardsView Answer on Stackoverflow
Solution 3 - SyntaxRashid IqbalView Answer on Stackoverflow
Solution 4 - SyntaxCoolMindView Answer on Stackoverflow
Solution 5 - SyntaxPalak DarjiView Answer on Stackoverflow
Solution 6 - SyntaxHitesh SahuView Answer on Stackoverflow
Solution 7 - SyntaxOsvel Alvarez JacominoView Answer on Stackoverflow
Solution 8 - SyntaxAlejandro NavasView Answer on Stackoverflow
Solution 9 - SyntaxSachini SamarasingheView Answer on Stackoverflow
Solution 10 - SyntaxRuhulView Answer on Stackoverflow
Solution 11 - SyntaxDeven MerView Answer on Stackoverflow
Solution 12 - SyntaxAbbin VargheseView Answer on Stackoverflow
Solution 13 - SyntaxHarish ReddyView Answer on Stackoverflow