Lateinit
An explanation of, and motivation for, lateinit
— — — — — — — — — — — — — — —
THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.
— — — — — — — — — — — — — — —

Tags: #KOTLIN FEATURE
This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.
It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.
Normally, properties declared as having a non-null type must be initialized by the constructor. However, it is often the case that doing so is not convenient. For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In these cases, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class. This is precisely the situation lateinit was designed to solve.
While it may seem very ad-hoc to include this as a language feature, it reflects the pragmatic realization of the Kotlin team that every single modern application larger than a small private project uses some sort of unit test framework & dependency-injection framework, the majority of which are based on Java. This means that every single modern application would suffer from this problem and Kotlin would not be compatible with frameworks such as Spring, severely decreasing Kotlin adoption and Java interop. Adding lateinit as a feature allows us to keep the good stuff in Kotlin, while still being realistic about the real world.
When using Spring, you will most often use lateinit when injecting a dependency differently than via constructor injection:

