class Age(var second: Int) {
var age: Int = 4
val isYoung // backing field is not generated.
get() = age < 30
// var isYoung // backing filed is generated automatically?????
// get() = age < 30 // Property must be initialized
}
Before I asked this question, I read a similar question, but I still have a question.
In the Backing Fields docs mention the following two cases in which a backing field is generated.
- when property uses the default implementation of at least one of the accessors.
- when a custom accessor references it through the field identifier.
Both the question on the link and my question are custom accessors, so it’s probably case 2
.
My question is, the document clearly states that it is created if the custom accessor
refers to the backing field
through the field identifier
. But why does the answer in the link say that it is created by default
in the var?
My question is, the document clearly states that it is created if the custom accessor
refers to the backing field
through the field identifier
. But why does the answer in the link say that it is automatically created in the var
?
I still can do val but I didn’t understand exactly why var can’t.
Sorry, please help me