What is primary constructor and secondary constructor in Kotlin

What is primary constructor and secondary constructor in Kotlin :

As we know that a constructor is used to construct a class object. In Kotlin, constructors are categorized into two types : primary and secondary. A Kotlin class can have only one primary constructor and multiple secondary constructors. In this tutorial post, we will learn about these constructs and their use cases.

Primary constructor :

The primary constructor is included with the class-name. For example :

class Student constructor(marks: Int) {}

The constructor keyword is actually not required. You can remove it if you want :

class Student(marks: Int) {}

Each of these class comes with one initializer block denoted by init. For example :

class Student(name: String) {
    init {
        println("Student name is ${name}")
    }
}

fun main(args: Array) {
    Student("John")
}

It will print the below output :

Student name is John

kotlin primary constructor As you can see that the code inside init method got executed when we have initialized the object. Also , the parameter that was used in the primary constructor was used in the initializer block. We can also have multiple initializer blocks for a single class. These blocks will be executed in the same order as they appear. For example :

class Student(name: String) {
    init {
        println("First init called")
    }

    init {
        println("Second init called")
    }

    init {
        println("Third init called")
    }
}

fun main(args: Array) {
    Student("John")
}

Output will be :

First init called
Second init called
Third init called

kotlin primary constructor

The parameters passed can be used in property initialization inside the class.

Secondary constructor :

Similar to primary constructors, we can have a different type constructors known as _secondary constructor _ in Kotlin. The common thing of secondary constructor with primary constructor is that these constructors are also prefixed with constructor keyword.

class Student{
    constructor(name: String){
        println("The name of the student is : ${name}")
    }
}

fun main(args: Array) {
    Student("John")
}

This is a simple example with only secondary constructor, without any primary constructors. If you also have primary constructor, we need to delegate the secondary constructor to the primary constructor.

class Student(name: String){
    constructor(name: String,age: Int) : this(name){
        println("The name of the student is : ${name}")
        println("The age of the student is : ${age}")
    }
}

fun main(args: Array) {
    Student("John",20)
}

Even if we have multiple secondary constructors, we need to delegate the secondary constructor to another secondary constructor or to the primary constructor. i.e. we need to delegate primary constructor directly or indirectly.

class Student(name: String){
    constructor(name: String,age: Int) : this(name){
    }

    constructor(name: String, age: Int, school: String) : this(name,age){
        println("The name of the student is : ${name}")
        println("The age of the student is : ${age}")
    }
}

fun main(args: Array) {
    Student("John",20,"ABC school")
}

Now, let’s take a look how the constructors are executed :

class Student(name: String){

    init{
        println("Inside first init")
    }

    constructor(name: String,age: Int) : this(name){
        println("Inside parent secondary constructor")
    }

    constructor(name: String, age: Int, school: String) : this(name,age){
        println("Inside child secondary constructor")
    }

    init{
        println("Inside second init")
    }
}

fun main(args: Array) {
    Student("John",20,"ABC school")
}

It will print the below output :

Inside first init
Inside second init
Inside parent secondary constructor
Inside child secondary constructor

kotlin secondary constructor As you can see that first primary constructor is executed. Because all secondary constructors are delegated from the primary constructor. All init{} blocks will be called first. Then the secondary constructors are executed parent-child order.

Conclusion :

This article has explained you the types of constructors in kotlin and how to use them . Also we have seen how code is executed if we use constructors. Try to run the examples explained above and if you have queries, drop one comment below.

You might also like :