Open In App

lateinit vs lazy Property in Kotlin

Last Updated : 31 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Kotlin is a modern-day gem that has proved itself to be a very useful language for android developers. It comes up with features that are easy to use and most importantly very understandable for developers of any background, be it Java, JavaScript, or even those who’re just beginning to explore Android development. With its excellent features, Kotlin has proved its worth in Android Developer’s Community and is getting all the love it deserves. So today we’re gonna discuss about two very important features – Lateinit and Lazy, which are initialization properties. So, before going into how it helps let’s understand the problem statement that it solves.

Problem 1

We wish to declare a variable but don’t want to initialize it during creation because we are certain that before going for the execution, it’ll definitely be initialized at some point in the program.

Solution

Here, we have two choices one is to declare a variable as nullable which often causes us trouble because there are functions that demand additional handling of nullable values. So here comes our very important and beautiful feature of Kotlin – the lateinit. The Lateinit in Kotlin is a trustworthy keyword that takes the responsibility on its head and says to the compiler:

let them declare it for now, I assure you they will initialize it before accessing or using it…

Kotlin




private lateinit var empName : String
private fun employee(empId: String){
      empName = departments.getEmpName(empId)
}


Just one more thing I wanna add is that lateinit has trusted you that you’ll initialize the variable so keep your word and if at any point you’re uncertain about initialization, just put a check using isInitialized. That’s all about lateinit. It was this simple and useful feature. Now let’s look at our second problem statement and see how lazy gets useful.

Problem 2

At times we have some classes whose object Initialization is very heavy and causes our whole program to be delayed.

Solution

Don’t go on the name it might be lazy by name but it proves its worth when used. So when object creation is stated to be initialized by using lazy, it doesn’t create the object until the object is called. This was one very important advantage but the second advantage is also interesting. Once the object is initialized we’ll be using the same object again when called, isn’t it interesting, I mean it removes all the overhead of repetitive object creation. 

Kotlin




class MainClass {
    
    private val heavyObj: HeavyClass by lazy {
        println("Heavy Object is initialized")
        HeavyClass()
    
  
    fun callObject() {
        println(heavyObj)
    }
}
  
fun main(args: Array<String>) {
    
    val mainClass = MainClass()
    println("MainClass is initialized")
    mainClass.callObj()
    mainClass.callObj()
      
}


In this above example, we can see that the object of the HeavyClass is created only when it is accessed and also the same object is there all over the main() function.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads