Learning Kotlin: ‘lateinit’ Modifier

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.

ex:

  • 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.


I love to meet/talk/discuss and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTube, Instagram and Facebook.

Check Out My YouTube Channel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.