
2
2
.
.
4
4
.
.
9
9
F
F
i
i
e
e
l
l
d
d
s
s
v
v
s
s
P
P
r
r
o
o
p
p
e
e
r
r
t
t
i
i
e
e
s
s
I
I
n
n
f
f
o
o
[
[
R
R
]
]
[
[
R
R
]
]
[
[
R
R
]
]
Field is a Constant or Variable declared inside a Class or Object.
Property is a named pair of get() and set() Methods which are accessed using Field Syntax
● name = "John" actually calls name.set("John")
● result = name actually calls name.get("John")
Content
● Backing Field (auto generated Field for storing Data)
● Backing Property (explicitly declared Field for storing Data)
● Backing Field prevents recursion (prevents calling getter and setter from within themselves)
● Kotlin Classes & Objects only support Properties? (in Kotlin Fields are considered Properties with hidden Methods)
B
B
a
a
c
c
k
k
i
i
n
n
g
g
F
F
i
i
e
e
l
l
d
d
Generally speaking Properties are just Methods and are not supposed to store data.
If they need to store data Property's get() and set() Methods should simply use some other Fields for that.
But Kotlin allows Property to store single value (if needed) into so called Backing Field in order to avoid having to
explicitly create some other Field for that purpose. But this Syntax is just for a special case when Property needs to store a
single Value. If Property needs to store multiple Values you still need to explicitly create multiple Fields for that.
This so called Backing Field can only be referenced inside Property's getter and setter using Keyword field.
If you use Keyword field, then this Fields will be created in Java generated Code to allow you to hold Data.
Backing Field
//DECALRE PROPERTY.
var name : String = ""
get() { return "Hello $field" }
set(value) { field = value }
B
B
a
a
c
c
k
k
i
i
n
n
g
g
P
P
r
r
o
o
p
p
e
e
r
r
t
t
y
y
Since Property is just a pair of Methods it doesn't necessarily need to be able to store data. But if it wants to store data
● it can store data inside Backing Field (by using Keyword field inside its Methods)
● it can store data inside any number of other Fields or Properties
If for some reason you don't want to use auto generated Backing Field, you can instead explicitly create an additional
Field and reference that one from get() and set() Methods to store and retrieve Data from it. Such Field is then called
Backing Property (because Kotlin implements Fields as Properties in Java generated Code).
So Backing Property is not some special kind of Property. It is more like a Coding Pattern when we decide that our
Property will use this Backing Property (Field) to store data (instead of using Backing Field).
This means that
● Backing Property can have any name you like (since you decide where your Property will be storing data)
● you can have multiple Backing Properties (if you decide that your Property should save Data in multiple places)
Using Backing Property also avoid recursion since inside get() and set() methods of our Property we are not calling those
same methods again, instead we will be calling get() and set() methods of the Backing Property.
Backing Property
//DECLARE BACKING PROPERTY (FIELD TO STORE DATA).
public var _name_ : String = ""
//DECLARE PROPERTY.
public var name : String
get() { return "Hello $_name_" }
set(value) { _name_ = value }