late init var foo: Bar()
fun goTime() {
foo = Bar()
println(foo)
}
Example var:
Copy code
var foo: Bar? = null
fun goTime() {
val foo = foo ?: Bar()
println(foo)
}
s
Sourabh Rawat
07/19/2021, 3:30 PM
Both has different usecase.
I tend to use val more unless I need mutablity.
The only place I would use lateinit var when I don't need mutability is when I can't provide it's initial value at the time of declaration. E.g.- field dependency injection, Android classes