Kotlin has two keywords for variables: val for a value that can’t be reassigned and var for one that can. Types are usually inferred. This guide also covers constants, global (top-level) variables and the Kotlin way to write a “static” variable.
val name = "Asha" // read-only, type inferred as String var count = 0 // mutable count += 1 val age: Int = 25 // explicit type
Page Contents
val vs var
| val | var | |
|---|---|---|
| Reassign? | No | Yes |
| Like Java | final variable | normal variable |
| Use when | Default choice | The value really changes |
val means the reference can’t change; the object may still be mutable: val list = mutableListOf(1); list.add(2) is fine.
Basic types
val isLoggedIn: Boolean = true val score: Int = 42 val big: Long = 3_000_000_000L val price: Float = 9.99f val pi: Double = 3.14159 val letter: Char = 'K' val title: String = "Kotlin"
Kotlin does not convert numbers implicitly: use score.toLong(), pi.toInt() and so on.
Nullable variables
var email: String? = null email = "a@b.com" println(email?.length ?: 0)
lateinit and lazy
lateinit var adapter: MyAdapter // var, non-null, set later (e.g. in onCreate)
val config: Config by lazy { // val, computed on first use
loadConfig()
}lateinit works only with non-primitive vars; accessing it before it’s set throws UninitializedPropertyAccessException.
Constants: const val
const val MAX_RETRIES = 3 const val BASE_URL = "https://api.example.com/"
const val is a compile-time constant. It must be top-level, in an object or in a companion object, and only primitives and String are allowed.
Global variable in Kotlin (top-level)
Declare the variable outside any class, at the top of a file. It is visible everywhere in the module (or only in that file if you mark it private):
// AppGlobals.kt package com.example.app const val APP_NAME = "Techpass" var isDarkMode = false // global mutable state – use sparingly private var requestCount = 0 // visible in this file only
In Android, prefer passing state through a ViewModel or dependency injection instead of global mutable variables – they are hard to test and can leak memory if they hold a Context.
Static variable in Kotlin (companion object)
Kotlin has no static keyword. Put class-level variables in a companion object:
class User(val name: String) {
companion object {
const val MAX_NAME_LENGTH = 30
var createdCount = 0
}
init { createdCount++ }
}
User("Asha"); User("Ravi")
println(User.createdCount) // 2
println(User.MAX_NAME_LENGTH) // 30Calling it from Java? Add @JvmField (or use const) so Java sees a real static field instead of a getter on User.Companion.
Getters and setters
var temperature = 0
set(value) { field = value.coerceIn(-50, 60) }
val isHot: Boolean
get() = temperature > 35FAQ
How do I declare a global variable in Kotlin?
Declare it at the top level of a file, outside any class: var counter = 0. Use const val for constants.
What is the difference between const val and val?
const val is fixed at compile time and limited to primitives and String; val is set at run time and can hold any type.
