Kotlin val vs var: Differences and When to Use Each

Short answer: in Kotlin, val declares a read-only variable that you can assign once, and var declares a mutable variable that you can reassign. Prefer val by default and use var only when the value must change.

val vs var at a glance

val var
Meaning Read-only reference Mutable reference
Can be reassigned No Yes
Java equivalent final variable Normal variable
Recommended Default choice Only when needed

Examples

val name = "Asha"
// name = "Ravi"        // Error: val cannot be reassigned

var counter = 0
counter = counter + 1    // OK
counter++                // OK

println(name)
println(counter)

Kotlin infers the type from the value, so you rarely need to write it. You can also add it yourself: val age: Int = 30.

val does not always mean immutable

A val stops you from pointing the variable at a different object. It does not stop the object itself from changing:

val list = mutableListOf(1, 2, 3)
list.add(4)            // OK: the list changes
// list = mutableListOf() // Error: cannot reassign the val

val fixed = listOf(1, 2, 3)
// fixed.add(4)        // Error: List has no add function

For true immutability, use read-only types such as List, Map and data classes with val properties.

val and var in classes

class User(val id: Int, var name: String)

val user = User(1, "Asha")
user.name = "Ravi"    // OK: name is var
// user.id = 2        // Error: id is val

A val property has only a getter. A var property has a getter and a setter.

val with a custom getter

class Rectangle(val width: Int, val height: Int) {
    val area: Int
        get() = width * height   // computed every time it is read
}

This val has no stored value. It is calculated each time you read it.

Related keywords

  • const val: a compile-time constant for primitives and strings. It must be top-level or inside an object or companion object. Example: const val BASE_URL = "https://example.com/".
  • lateinit var: a non-null var that you initialize later, before first use. Not allowed for primitive types such as Int.
  • by lazy: a val whose value is computed the first time it is accessed. Example: val config by lazy { loadConfig() }.

When to use which

  • Use val for anything that does not change after it is set. This makes code easier to read, safer with several threads and easier to reason about.
  • Use var for counters, loop-style state and values that really change over time.
  • In Jetpack Compose, state is usually declared as var text by remember { mutableStateOf("") }, because the delegate needs a var to allow assignment. See our LazyColumn example for state in practice.

Common mistakes

  • Using var everywhere. Start with val and change to var only if the compiler complains.
  • Thinking a val list cannot change. A val mutable list can still be modified.
  • Using const val for objects. It only works for primitives and Strings.
  • Using lateinit to avoid null checks without initializing. Accessing it before assignment throws UninitializedPropertyAccessException.

FAQ

What is the difference between val and var in Kotlin?

val is read-only and cannot be reassigned. var is mutable and can be reassigned.

Is val the same as final in Java?

Mostly yes. A val behaves like a Java final variable or property with only a getter.

Should I use val or var?

Use val by default and var only when the value must change.

What is the difference between val and const val?

val is assigned at runtime. const val is a compile-time constant and must be a primitive or String.

New to Kotlin? See our Kotlin vs Java guide and Kotlin tutorials.