In the section: Properties and Fields of the reference of kotlin, the following examples is written:
var allByDefault: Int? // error: explicit initializer required, default getter and setter implied
However, I test the code and there is no error in compiling and running. Here is my code"
fun main(args:Array<String>){
var allByDefault:Int?
}
So why does the documentation write:
error: explicit initializer required, default getter and setter implied
I have searched google for help but haven't found any result which can help me.
@toniedzwiedz 's answer has solved the issue. It's my fault. I mistook property and variable.
fun main(args:Array<String>){
var allByDefault:Int?
}
What you have here is a var
local to the main
method, not a property.
class MyClass {
//this is a property of MyClass that requires some means of initialization
var allByDefault: Int? // Error: Property must be initialized or be abstract
fun foo() {
var local: Int? // this is a local variable defined in the scope of foo, which is fine
// ...
}
}
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