How to implement variable without initializer ?
I found in Kotlin documentation:
val c: Int // Type required when no initializer is provided
c = 3 // deferred assignment
but this does not work. IDE requires to make a initializer.
If you're declaring a top-level property, you need to initialize it as part of the declaration. If you're declaring a local variable, you can initialize it later:
fun foo() {
val c: Int
c = 3
}
I just want to assign value to "C" in other class
val
can be used in two ways (counting 2 and 3 together):
For local variables, in which case assigning in other class makes no sense at all. The documentation you quote refers to this case.
For concrete properties, in which case they can be initialized separately from the declaration, but only in an init
block of the class they are declared in.
For abstract properties. But in this case you can't assign them from other class, but only implement these properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With