Kotlin

Constructors in Kotlin

Basically, the constructors in Kotlin can be categorized as the Primary Constructor and the Secondary Constructors. In other words, a class in Kotlin can have a primary constructor and one or more secondary constructors.

Creating Constructors in Kotlin

While a class in Kotlin can have one primary constructor, it can include several secondary constructors. For the purpose of creating secondary constructors, we need to use the constructor keyword.

Defining a Primary Constructor in a Class

In fact, the primary constructor in Kotlin is defined in the class header itself. The following syntax illustrates how to define a primary constructor in Kotlin.

class class-name(argument-list){
....
}

As shown above, the class-name is followed by a list of arguments enclosed in parentheses. This argument-list within parenthesis in the class header forms the primary constructor. As an illustration look at the following example.

class myclass(val a: Int, val b: Int){
//class members
}

In fact, the argument-list is not mandatory in the primary constructor. Therefore, the following code is also valid.

class MyClass_1() {
    var a: Int = 34
 }
fun main(args: Array<String>)
{
    val ob = MyClass_1()
    println("a = ${ob.a}")
}

Output

a=34

Further, a primary constructor doesn’t contain any code. Also, we can use the constructor keyword in declaring a primary construct as shown in the following code.

class MyClass_2 constructor(val i: Int){
    var j: Int
    init {
        j=i
    }
}
fun main(args: Array<String>)
{
    var ob1 =MyClass_2(378)
    println("i = ${ob1.j}")

}

Output

i=378

Also, the parameters of the primary constructor are accessible using the object of that class. The following code demonstrates it.

class MyClass_3(val p: Int, val q: Int){

}

fun main(args: Array<String>)
{
    var ob2 =MyClass_3(57, 94)
    println("p = ${ob2.p}, q = ${ob2.q}")
}

Output

p = 57, q = 94

Initialization of Instance Variables

In order to initialize instance variables using the parameters of the primary constructor, we can use the init block. The following example demonstrates it.

class MyClass_3(val p: Int, val q: Int){
    var i: Int
    var j: Int
    init{
        i=p
        j=q
    }
}

fun main(args: Array<String>)
{
    var ob2 =MyClass_3(57, 94)
    println("i = ${ob2.i}, j = ${ob2.j}")
}

Output

i = 57, j = 94

However, it is still possible to initialize the instance variables without using the init block. The following code demonstrates it.

class MyClass_3(val p: Int, val q: Int){
    var i: Int =p
    var j: Int =q
 }

fun main(args: Array<String>)
{
    var ob2 =MyClass_3(57, 94)
    println("i = ${ob2.i}, j = ${ob2.j}")
}

Output

i = 57, j = 94

Secondary Constructors in Kotlin

Apart from the primary constructor, a Kotlin class can also have a secondary constructor. Unlike the primary constructor, the secondary constructors are defined within the body of the class. Also, the secondary constructors can contain statements. In order to find examples on the secondary constructors click here.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *