Init in Kotlin

Let us understand the init block with an example.

In the person class, we have to check if the person is older than me or not. 
We can do it using,

    class Person(name: String, age: Int) {
        val isOlderThanMe = false
        val myAge = 25

        init {
            isOlderThanMe = age > myAge
        }
    }

Here, we have initialized two variables isOlderThanMe and myAge with a 
default value is false and 25 respectively.

Now, in the init block, we check the age from the primary constructor 
with myAge and assign the value to isOlderThanMe. i.e. if the age is
greater than 25, value assigned will be true else false.

To check this,
    var person = Person("Himanshu", 26)
    print(person.isOlderThanMe)
    
This will print the desired result. As when we have initialized the Person 
class with passing data name as Himanshu and age as 26. The init block will 
also get called as the object is created and the value of isOlderThanMe has 
been updated based on the condition.
android developer