knthmn
06/01/2021, 3:43 AMclass MainActivity : ComponentActivity() {
val foo by lazy { Foo() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
foo // initialize
}
}
I see the following benefits
• the "initialization" of the value is visually closer to where it is defined
• foo
is a val
Are there any disadvantages doing this? What do you guys prefer?knthmn
06/01/2021, 4:28 AMclass MainActivity : ComponentActivity() {
lateinit var foo: Foo
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
foo = Foo()
}
}
Jiddles
06/01/2021, 9:17 AM