lateinit

As the word says, it indicates that you will initialize before it's the first usage. We cannot use this property for primitive like classes, for example, Int, Float, and so on.

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 many times it's not possible. What if objects are getting initialized using Dependency Injection (DI). Test setup functions are another place where you want to initialize the object later:

 

    class HomeTimeLineFragmentPresenterTest {      lateinit var mock: TwitterCore      @SetUp fun setup() {        mock = // Initialize mock      }      @Test fun test() {        // Perform mock test      }    }
lateinit cannot be used with nullable and Java primitive ...

Get Kotlin Blueprints 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.