The lateinit
Keyword
The lateinit
keyword allows you to defer initializing a non-null variable until a later time. Use the lateinit
keyword when you know the value of a property will not be null once it is initialized.
private lateinit var mContacts: ArrayList<Contact>
Note: by
notNull()
can be used where lateinit will not work
var someProperty: SomeType by notNull()
public fun <T: Any> notNull(): ReadWriteProperty<Any?, T> =
NotNullVar()
Functions that provide most standard library delegates (including the notNull function) are defined in object delegates. To use them, we need to either refer to this object (Delegates.notNull()) or import it (import kotlin.properties.Delegates.notNull). We will assume in examples that this object is imported so we will omit reference to it.