Categories
kotlin

What the hell are Kotlin class constructors?

A Kotlin world

In a Kotlin world, there are primary and secondary constructors, let’s start with a Kotlin primary constructor, the recommended constructor to use:
https://gist.github.com/abdurahmanadilovic/727d25ac09290856b7764fa7d6419d00
That is it, constructor keyword is omitted and the declaration goes after the class name. Class properties are declared as arguments in the constructor definition, and since they are public by default you can access properties by instance.property syntax.
The main constructor cannot contain code, all initialization is done inside init blocks, and there can be multiple init blocks:
https://gist.github.com/abdurahmanadilovic/6dcb13d96f723f871a4acbcb01622345
Alongside the primary constructor, we have secondary constructors, and there can be multiple secondary constructors with different arguments just like in Java. Secondary constructors begin with a word constructor:
https://gist.github.com/abdurahmanadilovic/82f15c844bf5641b7a5163f24fdf672a
One thing to note is that before the second constructor is invoked, a primary constructor must be called with this keyword, that will force the execution the primary constructor and all init blocks to execute and all member initializations to be done, and then the secondary constructor will execute.
If you have multiple constructors you can delegate/call from one constructor to the next with this keyword, then pass the appropriate arguments.
https://gist.github.com/abdurahmanadilovic/d4a7ee4d15bd95038d60a2916783b830
So when to use the primary constructor, a secondary or an init block?

Usages

The primary constructor is the one you will use most of the time, and it has a nice short syntax, 90% of the time it is enough when declaring a new class.
Secondary constructors can be useful when a Kotlin code gets called from Java, an example of that would be when extending an Android View with a Kotlin class. In Kotlin you can declare default arguments, while in java you can’t, that is why secondary constructors are useful, you can create multiple constructors that require different parameters, a nice addition if you are calling Kotlin code from Java.
Init block can be used for, as its name suggests, default initializations that are required to run when an object gets created, because the primary constructor does not contain any expressions init blocks will serve as an extension to the primary constructor.

Order of execution

If you are like me, I was confused when I first saw init blocks, secondary constructors, primary constructor. What does execute first?
The first thing that gets executed is the primary constructor, then in order of placement either member initialization or init blocks, then secondary constructor:
https://gist.github.com/abdurahmanadilovic/b6daae960d8c14fbda3bdad8ef174e2e

Conclusion

As seen in the examples above, Kotlin offers multiple ways of doing things, from a simple primary constructor to multiple constructors that offer better interoperability with Java. Also, the init blocks that can provide a clear way of specifying what is important and what has to be run once an object is created.
If you find the article interesting comment below or share on social media, thanks.

Leave a Reply

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