Kotlin: Variable or Object Initialization | Erselan Khan

Table of contents

No heading

No headings in the article.

Today we will show you how we can Initialize an Object or Variable in the Kotlin programming language. But before moving forward, I would like to ask you to please follow my medium account to get the latest updates about Android and other tech-related topics and also check out my previous part of this series here Link

Let’s start with some basic examples of variable initialization in the Kotlin language:

// int value
var intValue: Int = 1
// but we can also initialize variable without defining it's type
var intValue = 1
// if your want to initialize variable for only one time
val intValue = 1

As you can see above that we can initialize a variable without defining its type or we can also define its type too, it’s totally up to your choice.

We can also initialize variables with var and val, but you might think that what’s the difference between var and val right? So basically, we can reinitialize the var type variable but in the case of val, we can’t reinitialize it.

Here are some more examples for defining variables:

fun main() {

    // string value
    var stringValue: String = "Erselan Khan"

    println("String value $stringValue")

    // int value
    var intValue: Int = 1

    println("Int value $intValue")

    // float value
    var floatValue: Float = 1f

    println("Float value $floatValue")

    // boolean value
    var booleanValue: Boolean = false

    println("Boolean value $booleanValue")

    // double value
    var doubleValue: Double = 1.0

    println("Double value $doubleValue")

}

Now we can show you how we can initialize Objects in the Kotlin language:

fun main() {
    /*
    Simple object initialization
    */
    val objectInitialization = ObjectInitialization()
    objectInitialization.printMyName("Erselan Khan")
}
class ObjectInitialization {
    fun printMyName(name: String) {
        println("My name is $name")
    }
}

We can also use lateinit with Objects to initialize it later like below:

fun main() {

    /*
    Simple object initialization
    */
    val objectInitialization = ObjectInitialization()
   objectInitialization.addTwoNumbers(firstNumber = 2, 
secondNumber = 2)
}

class ObjectInitialization {

    /*
    Must define the type of lateinit i.e "Number"
    */
    lateinit var initializationLateInitObject: Number

    /*
    initializing lateinit variable
    */
    fun addTwoNumbers(firstNumber: Int, secondNumber: Int) {
        initializationLateInitObject = firstNumber + secondNumber
        println("Print value: ${initializationLateInitObject.toInt()}")
    }
}

As we are not initializing the lateinit variable at the time of defining, so it might be produced NullPointerException, So we need to check the variable initialization before accessing like below:

/*
accessing lateinit variable after checking its initialization
*/
fun printLateInitVariableValue() {
    if (this::initializationLateInitObject.isInitialized) {
        println("Print value: ${initializationLateInitObject.toInt()}")
    }
}

Here is the full example for defining or initializing an Objects:

fun main() {

    /*
    Simple object initialization
    */
    val objectInitialization = ObjectInitialization()
    objectInitialization.printMyName("Erselan Khan")
    objectInitialization.addTwoNumbers(firstNumber = 2, secondNumber = 2)
}

class ObjectInitialization {

    /*
    Must define the type of lateinit i.e "Number"
    */
    lateinit var initializationLateInitObject: Number

    fun printMyName(name: String) {
        println("My name is $name")
    }

    /*
    initializing lateinit variable
    */
    fun addTwoNumbers(firstNumber: Int, secondNumber: Int) {
        initializationLateInitObject = firstNumber + secondNumber
        printLateInitVariableValue()
    }

    /*
    accessing lateinit variable after checking it's initialization
    */
    fun printLateInitVariableValue() {
        if (this::initializationLateInitObject.isInitialized) {
            println("Print value: ${initializationLateInitObject.toInt()}")
        }
    }
}

That’s it for now. I will cover more topics on Android, Java, Kotlin, and Springboot in my upcoming articles.

In case you missed:

  1. erselankhan.medium.com/android-kotlin-vs-ja..
  1. towardsdev.com/android-kotlin-vs-java-serie..

  2. medium.com/bazaar-tech/dynamically-update-r..

Show your love by sharing this article with your fellow developers and also following my Medium account.

(Again, the source for this demo is on github.com/arsalankhan994/kotlin-examples. Follow me for more content about Android, Kotlin, and other technologies. If you have any questions, go ahead and ask me here or email me at and I’ll do my best to respond.)