Lateinit

As the word says, it indicates that you will initialize before the first usage. We cannot use this property for primitive classes, such as int or float. Because for every lateinit object, Kotlin uses null to mark that object and throws an exception when trying to access them, and for primitive types, there is no such value. And it always makes sense to initialize them with their default values, such as 0 and true/false.

You can initialize this type of object in any place where it's accessible. Every non-null object should be initialized at the time of declaration. But often it's not possible. What if the objects are getting initialized using Dependency Injection (DI). Test setup functions are another place where you want to initialize ...

Get Hands-On Serverless Applications with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.