June 2018
Intermediate to advanced
316 pages
6h 34m
English
If you need to access a field of a property inside a class in which it's declared, you can use backing properties. If you only need to access a field from the getter or setter of its property, it's enough to use backing fields. However, if you want to access a field somewhere else, you should use backing properties. Let's look at the following example:
class Button { private var _text: String? = null var text: String set(value) { println(value) _text = value } get() { return _text + _text } fun printText() { println(_text) }}
In the preceding snippet, the _text variable is a field of the text property. To make sure of this, you can use the Kotlin Bytecode inspector:
public final class Button { private String _text; @NotNull ...
Read now
Unlock full access