This Kotlin tutorial explores the lateinit modifier and gives several examples of how you can use lateinit in Android or other Kotlin projects.
lateinit allows you to defer initialization of non-null values so they can still be defined as non-null, but can be assigned later as in many dependency injection or test frameworks.
- lateinit var userId:String
This is a non-null type, without a non-null initializer. Without lateinit, this would not be valid Kotlin code.
- 0:17 What is lateinit used for?
- 0:40 Why is lateinit useful?
- 1:23 How to use lateinit?
- 1:30 Android dependency injection example
- 3:00 Testing example
- 4:05 Where can lateinit be used?
Reference Documentation: https://kotlinlang.org/docs/reference…
Thoughts or questions about the lateinit modifier in Kotlin? Comment below or share on social media. I’d love to continue the conversation.
Categories: Learning Kotlin