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.
lateinitvar name: String
funinitializeName(){
name ="John"
}
funmain(){
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
funsetAge(newAge: Int){
age = newAge
}
funmain(){
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 (