Skip to content
Gallery
Kotlin
Share
Explore
Kotlin Documenation

icon picker
Null-able Types



A. Using lateinit

The lateinit keyword is used for variables that are mutable (var) and will be initialized later. It can only be used with non-nullable types and must be initialized before its first use.
lateinit var name: String

fun initializeName() {
name = "John"
}

fun main() {
initializeName()
println(name) // John
}

B. Using Nullable Types

If you want to declare a variable that might not be initialized immediately, you can use a nullable type by adding a ? after the type. This allows the variable to be null initially.
var age: Int? = null

fun setAge(newAge: Int) {
age = newAge
}

fun main() {
setAge(25)
println(age) // 25
}

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.