lazy Property in Kotlin

This post was originally posted at https://agrawalsuneet.github.io/blogs/lazy-property-in-kotlin/ and reposted on Medium on 03rd Dec 2021.
  1. That object/property/variable is dependent on another object to initialise first and use its reference.
  2. The flow is such that we need that object only in a certain condition.
  1. Its var that means it’s mutable
  2. We (developer) need to remember to initialise it.
  3. We can’t have a custom getter to the lateinit property
data class User(val id : Long,
val username : String)

val user : User by lazy {
//can do other initialisation here
User(id = 1001, username = "ballu")
}

Please continue reading at https://agrawalsuneet.github.io/blogs/lazy-property-in-kotlin/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store