Kotlin provides the facility of lazy initialization, which specifies that your variable will not be initialized unless you use that variable in your code. It will be initialized only once. After that, you use the same value.
In lazy initialization, the
lazy()
function is used 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"
} ​