# What Is the Difference Between a Backing Field and a Backing Property in Kotlin?

> **In short:** A backing field is the storage Kotlin creates for a property. You reach it with `field` inside that property's getter or setter. A backing property is a second, private property you write yourself, like `_uiState`, when the stored value needs a different type than the one you show. Since Kotlin 2.4, an explicit backing field does the most common backing-property job in one declaration.

Most ViewModels have two names for one value.

Say a screen's ViewModel holds `private val _uiState = MutableStateFlow(UiState())` and `val uiState: StateFlow<UiState> = _uiState`. In another class, a setter says `field = value`, and you can't find where `field` was declared. Both are ways to store a property's value. They solve different problems.

This post covers what a backing field is, the setter bug that crashes with `StackOverflowError`, why `_uiState` exists, and what Kotlin 2.4 changed.

## What is a backing field?

A property in Kotlin is a getter, maybe a setter, and maybe a place to store the value. That place is the backing field. The compiler creates it [when the property needs to store something](https://kotlinlang.org/docs/properties.html#backing-fields): when you use the default getter or setter, or when your custom getter or setter uses `field`.

```kotlin
class Player {
    var volume: Int = 50
        set(value) {
            field = value.coerceIn(0, 100)
        }

    val isMuted: Boolean
        get() = volume == 0
}
```

`volume` keeps its value in a backing field, and the setter keeps it between 0 and 100. Setting `150` stores `100`. `isMuted` has no backing field. It computes the answer on every read, so there is nothing to store.

The bytecode shows it (Kotlin 2.4.21, from `javap`):

```java
public final class Player {
  private int volume;
  public final int getVolume();
  public final void setVolume(int);
  public final boolean isMuted();
}
```

One field for `volume`, none for `isMuted`. If you try to give `isMuted` a starting value, the compiler says "Initializer is prohibited here because this property has no backing field."

`field` only exists inside that property's getter and setter. In a normal function it's "Unresolved reference 'field'."

## Why does my setter crash with StackOverflowError?

This is the most common mistake with custom setters. It looks almost right:

```kotlin
// Almost right: crashes the first time you set it
var volume: Int = 50
    set(value) {
        volume = value.coerceIn(0, 100)
    }

// Right: write to the backing field
var volume: Int = 50
    set(value) {
        field = value.coerceIn(0, 100)
    }
```

Inside the setter, `volume = ...` doesn't write the value. It calls the setter again, which calls it again, until the stack runs out. I ran it, and the first assignment threw `StackOverflowError`. `field` skips the setter and writes straight to the storage. The same goes for getters: `get() = volume` calls itself, `get() = field` doesn't.

## What is a backing property?

A backing field always has the same type as its property. Sometimes you want something else. On Android, the usual case is state: the ViewModel needs a `MutableStateFlow` it can change, and the screen should only get a `StateFlow` it can read.

You can't do that with `field`, so you add a second property yourself:

```kotlin
class SearchViewModel : ViewModel() {
    private val _uiState = MutableStateFlow(UiState())
    val uiState: StateFlow<UiState> = _uiState.asStateFlow()

    fun onSearch(query: String) {
        _uiState.update { it.copy(query = query) }
    }
}
```

`_uiState` is the backing property. It's private, so only this class can change the state. `uiState` is the public, read-only view. The leading underscore is just the [naming convention](https://kotlinlang.org/docs/coding-conventions.html#names-for-backing-properties) for this pattern.

Why `asStateFlow()` and not just `= _uiState`? I tried both. With `= _uiState`, the public property is the same `MutableStateFlow` object, and a caller can cast it back and change your state:

```kotlin
(viewModel.uiState as MutableStateFlow).value = UiState()
```

With `asStateFlow()`, `uiState` is a read-only wrapper, and the same cast throws `ClassCastException`. That costs one small extra object per ViewModel.

![Diagram: where the value lives. A backing field is storage Kotlin creates, reached with field, same type as the property. A backing property is a private _uiState you write, exposed as uiState with asStateFlow(). An explicit backing field in Kotlin 2.4 is one val whose field has a more specific type, MutableStateFlow.](https://imayankkumar.com/blog/kotlin-backing-field-vs-backing-property/kotlin-backing-field-vs-backing-property.png)

## What are explicit backing fields in Kotlin 2.4?

Writing two properties for one value is a lot of ceremony. Kotlin 2.4.0 made [explicit backing fields Stable](https://kotlinlang.org/docs/whatsnew24.html). You give the property a public type and give its backing field a different, more specific type:

```kotlin
class SearchViewModel : ViewModel() {
    val uiState: StateFlow<UiState>
        field = MutableStateFlow(UiState())

    fun onSearch(query: String) {
        uiState.update { it.copy(query = query) }
    }
}
```

Inside the class, the compiler [smart casts](https://kotlinlang.org/docs/properties.html#explicit-backing-fields) `uiState` to `MutableStateFlow`, so `update` works. Outside, callers only see `StateFlow`. In the bytecode there is one private `MutableStateFlow` field and one getter that returns `StateFlow`. No `_uiState`.

The rules are strict. The property must be a `val`, final, with no custom getter, and the field's type must be a subtype of the property's type. Break one and the compiler tells you, for example "Only 'val' properties with explicit backing fields are supported."

One thing to know: the getter returns the field itself, not a wrapper. So the cast from the last section still works. I checked: casting the new `uiState` to `MutableStateFlow` and setting a value changed the state. For callers that follow the types, that's fine. If you want to block the cast too, keep the backing property with `asStateFlow()`.

## Which one should you use?

| Feature | Backing field | Backing property | Explicit backing field |
| --- | --- | --- | --- |
| What is it? | Storage Kotlin creates | A private property you write, like `_uiState` | Storage with its own type, `field = ...` |
| How do you reach it? | `field`, only in the getter or setter | By name, anywhere in the class | The property name, smart cast inside the class |
| Different type from the property? | No | Yes, any type | Yes, a subtype |
| Works with `var` and custom getters? | Yes | Yes | No: `val` only, no custom getter |
| Fields in bytecode | One | Two with `asStateFlow()` | One |
| Blocks the cast back to mutable? | Not needed | Yes, with `asStateFlow()` | No |
| Use it for | Checks and side effects in a setter | Read-only views, or a custom getter over stored data | `MutableStateFlow` or `MutableList` exposed as read-only |

## Frequently asked questions

**Does every property have a backing field?**
No. A property gets one only if it stores a value: the default getter or setter, or `field` in a custom one. A property like `isMuted` that computes its value has none.

**Does a private backing property add a getter call?**
No. For a private property with the default getter, the compiler reads the field directly. In the bytecode `_uiState` is a private field with no `get_uiState()` method.

**Should I switch every `_uiState` to an explicit backing field?**
Only if you're on Kotlin 2.4 or newer, and you're fine with the cast point above. For new code it removes one property and the underscore. Old code doesn't need a rewrite: both compile to almost the same thing.

## What should you remember?

`field` is the storage behind one property, used inside its getter and setter. Write `field = value`, never the property's own name, or you get `StackOverflowError`. A backing property is a second, private property, used when the stored type should differ from the public one. Kotlin 2.4's explicit backing fields cover the `_uiState` case with one property, but they don't stop a cast like `asStateFlow()` does.

If you want to test yourself, try the [backing field interview questions](https://imayankkumar.com/interview/kotlin-backing-field-vs-backing-property/). This post is the eleventh topic in Stage 1 of my [Android roadmap](https://imayankkumar.com/roadmap/). The previous one is [val vs var](https://imayankkumar.com/blog/kotlin-val-vs-var/).

Have you moved any `_uiState` to an explicit backing field yet, or are you still on the two-property pattern?

---

*This post first appeared on* [*imayankkumar.com*](https://imayankkumar.com/blog/kotlin-backing-field-vs-backing-property/)*, where you'll find new posts first.*

