Kotlin

Kotlin “lateinit”

At times, you might not want to initialize a variable in Kotlin at the time of its declaration. Instead, you want to initialize it later whenever needed but essentially before it is accessed. This is where the “lateinit” keyword of Kotlin comes into play, and this article will mainly throw light on the usage of this variable in the Kotlin programming language in Ubuntu 20.04.

What Does the “lateinit” Keyword in Kotlin Do?

With the introduction’s help, you would know the “lateinit” keyword is used with a variable in Kotlin only when you wish to initialize that variable somewhere later in your script. However, you might consider making that variable “null” if you do not want to initialize it in the first place. Nevertheless, there still can be some situations in which you do not even want to make these variables null. You can use the “lateinit” keyword while declaring a variable for similar situations. The most significant advantage of using this keyword is that it does not allocate any memory to the variable before it is initialized. However, you must make sure that you initialize that variable later in your code before it is accessed. If you fail to do so, your compiler will generate some errors. Thus, you will not be allowed to execute your Kotlin script.

Cautions With the Usage of the “lateinit” Keyword in Kotlin:

After knowing the basic purpose of the “lateinit” keyword of the Kotlin programming language, you should also be aware of the cautions that you need to take before beginning to use the “lateinit” keyword in Kotlin. These cautions are summarized below:

  • The “lateinit” keyword must always be used with the “var” keyword and not with the “val” keyword.
  • The “lateinit” variables cannot be of the “null” type.
  • Before accessing it, you should initialize a “lateinit” variable because failing to do so will lead to exceptions and errors.

Examples of Using the “lateinit” Keyword in Kotlin:

To highlight the “lateinit” keyword usage in the Kotlin programming language in Ubuntu 20.04, we have implemented the following two examples for you:

Example # 1: Declaring and Using a Kotlin Variable as “lateinit”:

This example will teach you the simplest method of declaring a Kotlin variable as “lateinit” and then, using it to achieve some desired functionality. You can take a look at the Kotlin program shown in the image below to understand this:

In this Kotlin program, we have first used the “lateinit” keyword for declaring an object “emp” of the “Employee” class as “lateinit”. You can get that we have not implemented this class yet. Therefore, we will have to do it later in our script so that our compiler does not produce errors. We have created an instance “emp” of the “Employee” class in our “main()” function. Next, we have passed to it the name of the employee as a string and his experience in years as an integer. Then, we used the two “println” statements to print both of these parameters on the terminal. Finally, we have declared a data class named “Employee” that is capable of accepting two parameters, “name” and “experience” of string and integer types, respectively. This is where our “lateinit” object will finally be initialized.

After implementing this Kotlin script, we compiled it with the following command:

$ kotlinc lateinit.kt

Then, we used the command shown below to execute this script:

$ kotlin LateinitKt

The following image represents the output of this Kotlin program. You can see how we have successfully managed to use the “lateinit” keyword in Kotlin to print the name and experience of an employee.

Example # 2: Finding Out If a “lateinit” Variable Has Been Initialized or Not:

We have already mentioned that if you forget to initialize the “lateinit” variable within your Kotlin script, then it leads to compile-time errors. Therefore, there should be a way through which you can discover if a “lateinit” variable has been initialized or not. We will walk you through this method in the Kotlin script shown in the image below:

In this Kotlin script, we have first defined a class named “Course”. Within this class, we have declared a variable named “courseName” of string type as “lateinit”. Then, we have created a function of this class named “initializeName()”. Inside this function, we first wanted to check whether our “lateinit” variable has been initialized or not. For that, we have used the “this::courseName.isInitialized” notation.

Here, the “this” keyword corresponds to the present state of our “lateinit” variable, which at the moment is uninitialized. Then, we have used the scope resolution operator for accessing the current value of the “courseName” variable followed by the “isInitialized” function. Next, we have initialized the “courseName” variable with the name of a course. After that, we used the “isInitialized” function again to check the current state of the “lateinit” variable. Inside our “main()” function, we have created an instance of the “Course” class “c1”. With the help of this instance, we have called the “initializeName()” function.

The subsequent image shows the output of this Kotlin script. Initially, when our “lateinit” variable was uninitialized, the output returned by the “isInitialized” function was “false”. Whereas when we initialized the “lateinit” variable, the output of the very same function turned out to be “true”.

Conclusion:

In this guide, we first introduced you to the concept of late initialization, followed by the purpose of its existence in Kotlin. After giving you a brief overview of its working and the measures that must be taken to use it effectively, we shared two examples that further clarified this concept. By going through this guide, you would have a fair idea of the concept of late initialization in Kotlin in Ubuntu 20.04. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.