Properties

The val keyword defines read-only property.

The var keyword defines mutable property.

You can define properties within the class body or within primary constructor.

The lateinit keyword indicates that mutable property will be initialized later somewhere in code. The property type must not be primitive or nullable.

The lazy function allows to delegate initialization of read-only property to the first read operation.

You can initialize properties within init block.

class MyClass(val prop: String, var propMutable: Int?){
    
    val prop1: String // initialized in init block
    val prop2 = "Hello"
    val prop3 : String by lazy {"Lazy hello"}

    var propMutable1: Int? = null
    var propMutable2: Int? // initialized in init block
    lateinit var propMutable3: String
    // lateinit var propMutable3: Int primitive type
    // lateinit var propMutable3: String?  nullable type

    init {
        prop1="Hello World"
        propMutable2 = 2
    }
    
    fun method1(){
        propMutable3 = "Now initialized"
    }
}