Kotlin Constructor – Kotlin Secondary Constructor – Kotlin Init Block

In this tutorial, we shall learn about Kotlin Class, Kotlin Constructors – Kotlin Primary Constructor, Kotlin Secondary Constructor, and Kotlin init block with examples.

Kotlin Class – Kotlin Constructor – Kotlin Secondary Constructor – Kotlin Init Block

A class is the base of object oriented programming. We shall learn the following concepts:

Kotlin Class

A class is kind of a blue print for type of objects that belong to the class type.

Let us have a quick look into an example of a Kotlin Class to know the placement of Kotlin Constructors :


Structure of Kotlin Class

We shall look at the components that form a class in Kotlin

Kotlin Constructor - Kotlin Secondary Constructor - Kotlin Init Block

Structure of a class in kotlin

As mentioned in the picture above, a class has following three parts :

  • class keyword followed by class_name  class Person  – mandatory
  • Header of Class – contains the type parameters and an implicit Kotlin Primary Constructor constructor(var name: String, var age: Int)  – optional
  • Body of Class – contains class variables, Kotlin Secondary Constructors and functions/methods of class. The body of the class is enclosed with curly braces after the header of class – optional


Kotlin Constructors

There are two types of Kotlin Constructors. They are Kotlin Primary Constructor and Kotlin Secondary Constructor.

Kotlin Primary Constructor

There could be only one primary constructor for a class in Kotlin. The primary constructor comes right after the class name in the header part of the class.

A Kotlin Primary Constructor is highlighted in the following Example :


Visibility modifiers for Kotlin Primary Constructor

The default visibility on the primary constructor is public. However, the visibility can be changed to private, protected or internal. The syntax is given below :

class Person visibility_modifier constructor (var name: String, var age: Int)


Kotlin Secondary Constructor

Constructors that are written inside the Body of Class are called Secondary constructors.

Secondary Constructor should call primary constructor using this keyword. From the example of Kotlin class already given, the secondary constructor is :

This secondary constructor takes three variables, but calls primary constructor using : this(name, age)  to set the variables handled by the primary constructor.

The following example demonstrates the usage of secondary constructor :


Visibility modifiers for Kotlin Secondary Constructor

The default visibility of secondary constructor is public. However, the visibility can be changed to private, protected or internal. The syntax is given below :

visibility_modifier constructor (var name: String, var age: Int, var profession: String) : this(name, age){}


Kotlin init

If you observe the definition of primary constructor, there is no provision in the header to include some lines code for the primary constructor, except for the declaration of type variables. To fill this void, there is init block. But, care has to be taken that init block is run when the class variable is initialized. Hence, this init block is run for all the constructors irrespective of primary and secondary, and after the execution of primary constructor block. Init block is run with the context of primary constructor.

An example Kotlin program to demonstrate the working of Kotlin init block is given below :


Conclusion :

In this Kotlin Tutorial, we have learnt the structure of a class in Kotlin with an example, also the types of Kotlin Constructors we have for a class : primary constructor and secondary constructor, and the role of init block in aiding primary constructor.