Am sure you are confused with backing field term while learning Kotlin. I had spent time on this already and this article will help you with that. If you are a C++/Java programmer then you know the concepts of variables, which you can create in a class or outside a class. In OOPs, naming is different. As a C++ programmer, there is no difference between variables outside class or inside the class. For me, the class is a way of encapsulation.

Let’s take an example in Kotlin from where for the first time I faced the problem related to the backing field.

class MyClass{
var name : String
set(value) { name = value}
get() { return name}
}
fun main(){
var myClass = MyClass()
println(myClass.name)
}

Above code give me this error:

Exception in thread "main" java.lang.StackOverflowError
at MyClass.getName(kotlin2.kt:4)
at MyClass.getName(kotlin2.kt:4)
at MyClass.getName(kotlin2.kt:4)
at MyClass.getName(kotlin2.kt:4)
....

As a C++ programmer for me, nothing is wrong in the above code. For name variable am creating setter and getter which can set/get the value. There is one tool in IntelliJ Idea which if used can point to the exact problem. We know that Kotlin is ultimately translated to Bytecode like Java. What if we can see the Bytecode and convert it into Java code? Let us go to Tools -> Kotlin -> Show Kotlin Bytecode. After this, you will see the Bytecode which you can decompile into Java code.

Bytecode viewer in IntelliJ idea for Kotlin.

Here is Java code which we got after decompiling:

public final class MyClass {
@NotNull
public final String getName() {
return this.getName();
}
public final void setName(@NotNull String value) {
Intrinsics.checkParameterIsNotNull(value, "value");
this.setName(value);
}
}

When we observe the getName() function we see that it is getting called recursively and there is no variable to store name. So the stack overflow problem is understandable.

//Kotlin code
class MyClass{
var name : String = "Tarun"
}
//Java code from Bytecode of above kotlin code
public final class MyClass {
@NotNull
private String name = "Tarun";
@NotNull
public final String getName() {
return this.name;
}
public final void setName(@NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.name = var1;
}
}

From the above code, we see that default getter and setters are created and a variable is also created. We cannot access the underlying variable directly which is called the backing field. In Kotlin we use “property” instead of the “variable/field” as it means much more than a variable/field. To access the underlying backing field/variable in Kotlin we need to use the field in setter and getter. The field will allow you to access the underlying variable. If you are using the kotlin property name then it will recursively call those getter and setter.

The solution to our original problem:

class MyClass{
var name : String = "Tarun"
set(value) { field = value}
get() { return field}
}

Now am yet to understand why it was designed in this way. As a modern language, this should have been taken care as so many people would have to toil with this problem.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response