A
A
artem2021-07-26 23:22:36
Kotlin
artem, 2021-07-26 23:22:36

Why did the code from the official kotlin documentation give this example?

val c: Int  // Type required when no initializer is provided
c = 3       // deferred assignment


Why did the code from the official kotlin documentation give this example? As I googled, a variable cannot be uninitialized. At the very least, you need to write lateinit or assign null

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Romanov, 2021-07-26
@arteqrt

This is true for class fields, but not for local variables. Local variables can be initialized separately from the declaration. The compiler will make sure that this is done before the first call to it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question