lateinit var
whereas lazy val
A lateinit property can be reinitialized, but lazy property an only be initialized once.
lateinit can’t have custome getter
and setter
whereas lazy has custom getter
A lazy property has a block executed the first time that property is called.
Primitive types
lateinit properties can’t be of primitive types, but lazy properties can.
Thread safety
We can’t define thread safety in lateinit, but we can define in case of lazy.
val lazyUser : User? by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
User(id = 1, username = "agrawalsuneet")
}
Nullable type
A lazy property can be null
, but lateinit can’t be null
.