February 2018
Intermediate to advanced
350 pages
7h 35m
English
The lateinit keyword works only on the var properties. The Delegates.notNull() function works properly only with var properties, too.
So, what should we do when using val properties? Kotlin provides you with another delegation—lazy, that's meant for val properties only. But it works in a slightly different way.
Unlike lateinit and Delegates.notNull(), you must specify how you want to initialize the variable at the time of declaration. So, what's the benefit? The initialization will not be called until the variable is actually used. That's why this delegate is called lazy; it enables lazy initialization of properties.
The following is a code example:
val myLazyVal:String by lazy { println("Just Initialised") "My Lazy Val" ...Read now
Unlock full access