What is Kotlin Backing Field? & Field vs Property

As an Android application developer, I have been using Java since start. And in Java there is no such thing as Backing Fields. I was introduced to this concept while learning Kotlin.

Backing field is an autogenerated field for any property which can only be used inside the accessors(getter or setter) and will be present only if it uses the default implementation of at least one of the accessors, or if a custom accessor references it through the field identifier. This backing field is used to avoid the recursive call of an accessor which ultimately prevents the StackOverflowError.

Backing field is generated only if a property uses the default implementation of getter/setter. If you see the following code with perspective of Java. It looks correct. However in Kotlin it’ll throw StackOverFlow.

In Kotlin the above code snippet will throw StackOverflow because when we access or set property firstName or lastName the default accessor will be called. So in Kotlin user.firstName = “value” is same as Java’s user.setFirstName(“value”).

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.

See Also  Beginners Android Game Development Course

So when set(value) {firstName = “value”} is called, then a recursive call happens and compiler throws a StackOverflow exception because we are calling setter inside the setter.
Solution to this problem is to user backing fields. In Kotlin a backing field can be accessed using field keyword inside accessors. Take a look at corrected code snippet below.

See Also  Kotlin development: Setting up IDE, IntelliJ IDEA in macOS & Windows

As an Android application developer, I have been using Java since start. And in Java there is no such thing as Backing Fields. I was introduced to this concept while learning Kotlin.

Backing field is an autogenerated field for any property which can only be used inside the accessors(getter or setter) and will be present only if it uses the default implementation of at least one of the accessors, or if a custom accessor references it through the field identifier. This backing field is used to avoid the recursive call of an accessor which ultimately prevents the StackOverflowError.

Backing field is generated only if a property uses the default implementation of getter/setter. If you see the following code with perspective of Java. It looks correct. However in Kotlin it’ll throw StackOverFlow.

In Kotlin the above code snippet will throw StackOverflow because when we access or set property firstName or lastName the default accessor will be called. So in Kotlin user.firstName = “value” is same as Java’s user.setFirstName(“value”).

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.

See Also  Build Geolocation Tracking for Android Devices

So when set(value) {firstName = “value”} is called, then a recursive call happens and compiler throws a StackOverflow exception because we are calling setter inside the setter.
Solution to this problem is to user backing fields. In Kotlin a backing field can be accessed using field keyword inside accessors. Take a look at corrected code snippet below.

d93f3bce1660660e5de55f10d6df6f1a?s=96&d=mm&r=g
SyncSaShttps://syncsas.com
SyncSaS Technologies is specialized in creating and designing customized software. We have our own team that will be in charge of developing your softwares.

More from author

Related posts

Advertismentspot_img

Latest posts

How to increase YouTube Subscribers 2 Organic Strategy

How to increase YouTube Subscribers Being a YouTube personalities everyone wants the number of channel subscribers should multiply very quickly. Making YouTube video and uploading...

5 Ways to Increase Your YouTube Views

Increase Your YouTube Views Try going back and editing it to make it even better, and work on new videos. A helpful tip is to...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!