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 }
B
B
a
a
c
c
k
k
i
i
n
n
g
g
F
F
i
i
e
e
l
l
d
d
p
p
r
r
e
e
v
v
e
e
n
n
t
t
s
s
r
r
e
e
c
c
u
u
r
r
s
s
i
i
o
o
n
n
[
[
R
R
]
]
If inside the getter you reference Property name, you are actually calling getter again and you end up with infinite loop.
This is why you need to use Keyword field inside getter and setter to reference Property's Backing Field directly without
going through its Methods.
Or you can reference some Backing Property instead since that also wouldn't cause infinite recursion.
Preventing recursion
//DECALRE PROPERTY.
var name : String = ""
get() { return "Hello $name" } //$field would call get() again
set(value) { name = value } //field would call set() again
//REFERENCE PROPERTIES.
var age = john.age //Calls get()
john.age = "Bill" //Calls set()
K
K
o
o
t
t
l
l
i
i
n
n
C
C
l
l
a
a
s
s
s
s
e
e
s
s
&
&
O
O
b
b
j
j
e
e
c
c
t
t
s
s
o
o
n
n
l
l
y
y
s
s
u
u
p
p
p
p
o
o
r
r
t
t
P
P
r
r
o
o
p
p
e
e
r
r
t
t
i
i
e
e
s
s
?
?
Theoretically Kotlin only supports Properties. When you declare a Field without specifying getter and setter, this Kotlin
Code is converted into Java code with a private Field of the same Name and auto generated getter and setter.
So we are supposed to know how Kotlin Code gets converted into Java Code to make sense of Kotlin Syntax.
In Kotlin Code (which we can see) we see that we are only creating a Field
In generated Java Code (which we can't see) we see that we are working with a Property that has getter and setter
In other words while programing in Kotlin we are supposed to think that there are only Properties (and no Fields)
even though in our Code we only see a Field (since we have not declared any accessor Methods)
and even though Java generated Code might also have just a Field (without any auto generated accessor Methods)
(this might happen when having a private Field for optimization purposes depending on the Compiler)
Kotlin
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
var name : String = ""
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
//CREATE OBJECT.
var john = Person()
john.name = "John" //Set
var name = john.name //Get
//DISPLAY.
print(name)
}
Java generated Code
//===========================================================================================================
//CLASS: Person
//===========================================================================================================
class Person {
//PRIVATE FIELD.
private String name = "";
//PUBLIC GETTER.
public String getName() { return this.name; }
// PUBLIC SETTER.
public void setName(String name) { this.name = name; }
}
//===========================================================================================================
//CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
//CREATE OBJECT.
Person john = new Person();
john.setName("John"); //Set
String name = john.getName(); //Get
//DISPLAY.
System.out.println(name);
}
}