In this blog, we will learn about the lateinit
vs lazy
properties in Kotlin.
In Kotlin, there are two ways to initialize a property that is not available at the time of object creation: lateinit
and lazy
. Both of these keywords allow you to postpone the initialization of a property until it is actually needed, but they work in slightly different ways. In this blog, we will compare and contrast lateinit
vs lazy
and discuss when to use each.
lateinit
lateinit
is a keyword that allows you to declare a non-null property without initializing it immediately. You can then initialize the property at a later time, as long as you do so before you try to access it. The most common use case for lateinit
is when we are working with dependency injection frameworks like Dagger or Koin, where the initialization of certain properties might be deferred until runtime.
lateinit var myObject: MyObject
fun initializeMyObject() {
myObject = MyObject()
}
There are a few things to keep in mind when using lateinit
:
The property must be a non-null type, or else you'll get a compilation error.
You can only use
lateinit
with properties that are declared at the class level (i.e., not local variables).If you try to access a
lateinit
property before it has been initialized, you'll get alateinit
exception at runtime.
lazy
lazy
is a keyword that allows you to declare a property whose initialization is deferred until the first time it is accessed. The property is then cached, so subsequent accesses to the property will return the same value without re-initializing it. This is useful when you have expensive initialization logic that you don't want to perform unnecessarily.
val myObject: MyObject by lazy {
MyObject()
}
When using lazy
, you should keep the following in mind:
The property must be declared with
val
, notvar
.You can only use
lazy
with properties that are declared at the class level (i.e., not local variables).The initialization logic should be thread-safe, as
lazy
properties are only initialized once and can be accessed by multiple threads.
When to use each
So, when should we use lateinit
vs lazy
? Here are some guidelines:
Use
lateinit
when we need to declare a non-null property that cannot be initialized immediately, but we know it will be initialized before it is accessed. For example, if we are working with a dependency injection framework, we might uselateinit
for properties that are injected at runtime.Use
lazy
when we need to declare a property that might not be accessed immediately, and we want to avoid unnecessary initialization. For example, if we have an expensive operation that calculates a property value, we might uselazy
to ensure that it is only calculated when it is actually needed.
In summary, lateinit
and lazy
are two powerful tools that can help you write cleaner and more efficient Kotlin code. By understanding the differences between the two and knowing when to use each, we can write code that is both more correct and more performant.
If you want to read more content like this, please go check out my profile.