Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will `var allByDefault: Int ?` cause error?

Tags:

kotlin

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.

like image 511
A.Chao Avatar asked Dec 24 '17 10:12

A.Chao


1 Answers

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
       // ...
    }
}
like image 196
toniedzwiedz Avatar answered Sep 22 '22 17:09

toniedzwiedz