Explain lazy initialization in Kotlin

Technology CommunityCategory: KotlinExplain lazy initialization in Kotlin
VietMX Staff asked 3 years ago

lazy means lazy initialization. Your variable will not be initialized unless you use that variable in your code. It will be initialized only once after that we always use the same value.

lazy() is a function that takes a lambda and returns an instance of lazy which can serve as a delegate for implementing a lazy property: the first call to get() executes the lambda passed to lazy() and remembers the result, subsequent calls to get()simply return the remembered result.

val test: String by lazy {
    val testString = "some value"
}