Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Follow publication

Kotlin ⚡️: Variable or Object Initialization | Erselan Khan

--

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:

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 it's initialization
*/

fun printLateInitVariableValue() {
if (this::initializationLateInitObject.isInitialized) {
println("Print value: ${initializationLateInitObject.toInt()}")
}
}

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

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. https://erselankhan.medium.com/android-kotlin-vs-java-series-part-4-erselan-khan-f4fe57fdc2ae
  2. https://towardsdev.com/android-kotlin-vs-java-series-part-5-erselan-khan-90b5a1dfcd73
  3. https://medium.com/bazaar-tech/dynamically-update-refresh-reload-viewpager2-fragments-588fcbd6f859

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 https://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 arsalankhan994@gmail.com and I’ll do my best to respond.)

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Written by Erselan Khan

Software Engineer | Android Developer | Kotlin | Springboot | Java | Technical Content Writer | Motivational Speaker

No responses yet

Write a response