Updated March 2, 2023
Introduction to Kotlin Lazy
Kotlin lazy contains many features; by using this feature, we can take advantage of writing better application. The kotlin lazy is a very useful and important feature of property initialization; we know when we are using which property. In kotlin, there is a number of classes whose initialization of the object is very heavy, and it will take time to result in a delay in the whole class process creation. Sometimes we need to construct the object which contains the initialization of cumbersome. The concept of kotlin lazy initialization is designed to prevent the initialization of unnecessary objects as per the process.
What is Kotlin Lazy?
Kotlin lazy is nothing but the function which was taking a lambda and returns an instance of lazy, which can serve as implementing the property of lazy. The first call of getting is executing the lambda, which was passed through the lazy function, and we need to remember the result. The call of subsequent will return the remembered result. The evaluation of the property of lazy is synchronized, and the value is computed only in one thread if the initialization is not required to allow multiple threads to execute simultaneously for the parameter of lazy.
How does it Work?
For the efficient management of memory, kotlin introduced the new feature name lazy initialization. At the time a lazy keyword is used, the object is created when it was called; otherwise, it will not contain any creation of the object. The lazy function will be taking the lambda and returning the lazy instance, which was serving the delegate of the lazy properties on which we are applying. This is used to prevent the unnecessary initialization of the object. Lazy is used with the nonnullable variables. The variable only val or var is not allowed in kotlin lazy. We can initialize the object only once; after that, we receive the value from the memory of the cache. The object is not initialized until it was not been used in an application.
The below example shows how we can use kotlin lazy in our application. In the below example, we are declaring the lazy variable as studName, and we can see that the call of this variable happens only once at the time the value is initialized; we need to remember that value is assigned by using the lazy initialization, we cannot reassign the values. In the below example, we have defined the class name as kotlin.
Code:
class kotlin {
val studName: String by lazy {
println ("kotlin lazy declaration");
"www.example.com"
}
}
fun main () {
var obj = kotlin ();
println (obj.studName);
println ("We are calling the same object again" + obj.studName);
}
Output:
In the below example, we have defined the class name as lazy; then we need to create an object of lazy in some order class named lazy1.
Code:
class lazy1 {
private val lazyObject: lazy = lazy ()
}
We are creating the lazy object, and this will result in the slow and delayed creation of the lazy class. There may be multiple cases where we have not to need the lazy object. In the same case, the lazy keyword is more helpful.
Code:
class lazy1 {
private val lazyObject: lazy by lazy {
lazy ()
} }
The benefit of using lazy in kotlin is that object will be created only when it is called; otherwise, it is not created. The other benefit of using lazy is that once we have initialized the object, we can use the same object again in our program. Basically, lazy is only used when we want to access some property of read-only because we have access to the same object throughout.
Kotlin Lazy Using Property
Object creation is a heavy process in kotlin; when we are creating a class object, all the private and public properties of that class will be initialized into the constructor. Every variable inside the class initialization will require a certain amount of time for allocating memory to the heap and holding its reference onto the stack. Sometimes we have no need to initialize all objects at the time of class creation because the object property is dependent on another object to initialize first and use this reference.
In kotlin, we have multiple features that can help us delay that object initialization as per requirement. Lazy initialization is the object creation delegation the first time we are calling any object. The reference of the object is created, but the object is not created. The object is created when the first time the object is accessed, and the same reference is used in next time. Lazy is the function that was defined in a kotlin package which was taking lambda or higher order function as a parameter and returning the object of lazy. The passed lambda or a higher order function is returning the lazy object. By default, the lazy property is synchronized, meaning a single thread is computing its value and all the threads are using the same value as follows. We are using synchronized to ensure the safety of the thread; only a single thread is initializing the value.
Code:
public actual kotlin
{
…..
}
Output:
Kotlin by Lazy and Lateinit
The library of kotlin provides two different types of access modifiers for the declaration of property. For creating the latentit variable, we need to add the keyword name as lateinit as an access modifier for that variable.
Below is the set of conditions that we need to be followed to use the lateinit into the kotlin:
- Use the lateinit into the variable of mutable, which means we need to use the var keyword with lateinit.
- Lateinit is allowed in nonnullable data types.
- Lateinit is not working with the data type of primitive.
- Lateinit is used when the variable property does not contain the setter and getter methods.
The below example shows kotlin by lazy and lateinit as follows. We are using laze method as follows.
Code:
class lateinit
{
lateinit var name : String
fun init ()
{
if (this::name.isInitialized)
println ("kotlin lazy");
else {
name = "www.example.com/"
println(this.name)
}
}
}
fun main() {
var obj=lateinit();
obj.init ();
}
Output:
Conclusion
Kotlin lazy is nothing but the function which was taking a lambda and returns an instance of lazy, which can serve as implementing the property of lazy. The kotlin lazy is a very useful and important feature of property initialization; we know when to use which property.
Recommended Articles
This is a guide to Kotlin Lazy. Here we discuss the introduction, working, and kotlin lazy using property. You may also have a look at the following articles to learn more –