Hi ,
Here is my last topic of this Kotlin Basic Series . In this post I will talk about lateinit keyword & lazy delegation
lateinit keyword :
It is used at the time of variable declaration , there are some rules :
lazy is lazy initialization.
lazy() is a function that takes a lambda and returns an instance of lazy which can serve as a delegate for implementing a lazy property:
So that's it for this post and for this series as well . I tried to explore and cover all the basic topics of Kotlin here, and with the help of these topics one can understand and develop android applications in Kotlin as well .
In next post , I will explore a very interesting & essential topic i.e. Accessibility , till then Happy Coding 😀😀
Here is my last topic of this Kotlin Basic Series . In this post I will talk about lateinit keyword & lazy delegation
lateinit keyword :
It is used at the time of variable declaration , there are some rules :
- It is used with mutable type of variables i.e. var
- lateinit var name : String ------ Allowed
- lateinit val name : String ------ Not Allowed
- Allowed only non-nullable data types
- lateinit var name : String ------ Allowed
- lateinit val name : String? ------ Not Allowed
- The value must be assigned before it is used , other it throws UnintializedPropertyAccessException
lateinit var name : String
lazy Delegation :
lazy() is a function that takes a lambda and returns an instance of lazy which can serve as a delegate for implementing a lazy property:
- The first call to get() executes the lambda passed to lazy() and remembers the result, subsequent calls to get() simply return the remembered result.
- It is thread safe , it is initialized in thread where it is used for the first time , other threads used the same value remembered result which is stored in cache .
- It can be used with var & val both
- It can be used with nullable & non-nullable values both
public class Example{ val name: String by lazy { "Android" } }
So that's it for this post and for this series as well . I tried to explore and cover all the basic topics of Kotlin here, and with the help of these topics one can understand and develop android applications in Kotlin as well .
In next post , I will explore a very interesting & essential topic i.e. Accessibility , till then Happy Coding 😀😀
No comments:
Post a Comment