https://kotlinlang.org logo
Title
f

Florian

11/23/2019, 6:53 PM
besides
lateinit
, all properties of a class need to be initialized when the object is constructed, right?
m

Mark Murphy

11/23/2019, 7:46 PM
By something, yes.
lateinit
and
by lazy {}
are ways to supply the initial value later. Technically
by lazy {}
initializes the property up front, but with a property delegate, where the effective value is determined later by evaluating the lambda.
f

Florian

11/23/2019, 9:06 PM
thanks
we can initialize them at declaration in the class body, inlined into the primary constructor, in the secondary constructor if there is no primary constructor and inside an init block
anything else?
m

Mark Murphy

11/23/2019, 9:08 PM
Those are certainly the big ones. I cannot think of any other scenarios, but there are enough dusty corners of Kotlin syntax that there might be one that I'm forgetting. 😁
f

Florian

11/23/2019, 9:11 PM
yea it's a little bit confusing for me
v

varun

11/24/2019, 2:45 AM
One thing is that people coming from Java might not consider a property having the value
null
as initialized but in Kotlin even a nullable property won't by default have the value of
null
. So initializing them with
null
would also count as initializing a property.
f

Florian

11/24/2019, 9:45 AM
right thats a good point