Late-Initialized Properties in Kotlin

There can be two ways to declare and initialize a var property

var variable : CustomClass = CustomClass() 
var variable : CustomClass? = null

The first property is initialized while declaration itself and doesn’t require a null check (?.) while using it.

This post was originally posted at and reposted on Medium on 10th Nov 2017.

But in the second type, the property is initialized with a null value and will require a null check (?.) always while using it.


There can be a use case where we don’t want to initialize the property while declaration but also at the same time we don’t want a null check on that property every time as we are sure that while using that property, it’s value will be not null for sure. Conditions like initialization of property via dependency injection or in a setup method of unit test.

To handle this kind of cases, we can mark the property with the ‘lateinit’ modifier

lateinit var variable : CustomClass

the property ‘variable’ can be initialized later in the constructor or in any method before accessing it. Also, it doesn’t require a null check (!!) every time while using it.

variable = CustomClass()

Limitations of Late-Initialized Properties

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store