A constructor is responsible to initialize class properties and it’s called whenever a new object of a class is created. Many languages like Java and C++ allows you to define constructors in a straight-forward manner but Kotlin is slightly different. Don’t worry, it’s better in fact. Kotlin has two types of Constructors:
- Primary Constructor: Defined in the class header
- Secondary Constructor: Defined in the class body
You can also check the video tutorial:
Primary Constructor
This is the standard way to use constructors in Kotlin. In Java, we had to define constructors inside the class body. But Kotlin provides a more concise syntax and we don’t even need to look inside the class body to find the constructor. You can define it like this:
// Primary Constructor is defined in the class header itself // Standard way to define a constructor without keyword (Primary Constructor) class Person(var name: String) { fun tellName() { println("My name is $name") } }
Don’t forget to put var/val
(Mutable/Immutable) before the constructor field name. This will make your parameters accessible throughout the class and auto-generated getters and setters (only if it’s var
).
You can use a constructor
keyword as well. But it’s optional.
class Alien constructor(var name: String) { fun tellName() { println("My name is $name") } }
Secondary Constructor
This isn’t much used because the primary constructor is so much elegant. The possible use-case scenario is in Inheritance when you need to change the initializing behavior of the extended class.
class SuperHuman { private var name: String constructor(name: String) { this.name = name } fun tellName() { println("My name is $name") } }
A secondary constructor is defined using constructor
keyword. You can have multiple secondary constructors like this:
class SuperAlien { private var name: String constructor(name: String) { this.name = name } constructor(name: String, age: String) : this(name) { println("Age is just a number") } fun tellName() { println("My name is $name") } }
Just notice that how : this(name)
is used to call another constructor. This is a little different from Java where we call it inside the constructor block.
Init Blocks
Init blocks can be thought as a place to do logic computations on primary constructor’s parameters since class heading isn’t a place to perform such computations. You can have multiple init blocks and they are called in the order they appear.
class YetAnotherPerson(var name: String) { init { // Do any logic computations here println("Init block called") } init { // Init blocks are called in the order they appear println("Init block called yet again") } fun tellName() { println("My name is $name") } }
Primary Constructor + Secondary Constructor
You can use both the constructors at the same time. Here’s how:
class YetAnotherAlien(var name: String) { init { // Do any logic computations here println("Init block called") } init { // Init blocks are called in the order they appear println("Init block called yet again") } constructor() : this("(No name passed)"){ println("Empty constructor called") } fun tellName() { println("My name is $name") } }
The secondary constructor is calling the primary one by using this
keyword.
So that’s it in this tutorial. You can check out the full code on GitHub.
We’ll be doing more tutorials on Kotlin. Please provide your feedback in the comments and let us know how we are doing.