When and How to use Lateinit or By Lazy in Kotlin

Learn how to better initialize your values in kotlin.

When and How to use Lateinit or By Lazy in Kotlin

Hello Engineers...

In Kotlin we are provided with preferences when it comes to initialization, you can either initialize your variable using a **Var **for the mutable values,Val for the immutable values, lateinit, or by Lazy depending on your use cases. So in this article, we'll be looking at two of the most interesting features in Kotlin syntax -lateinit and lazy initialization, how to use them, when to use them, and their differences, so let's dive in.

Lateinit

Lateinit means late initialization

Lateinit is basically used when you don't want to initialize a variable beforehand and will initialize it later on and must guarantee initialization before usage. Accessing lateinit before initialization will throw a UnInitializedPropertyAccessException meaning it must be initialized before usage.

Lateinit can only be used with mutable variables var and not val and only accepts non nullable data types.

 lateinit var name: String       //Allowed
 lateinit val name: String       //Not Allowed
 lateinit var name: String  //Allowed
 lateinit var name: String?  //Not Allowed

When to use lateinit

When you need to create a variable but you don't want to initialize it at the time of declaration but you are sure it will be initialized at a later time before its accessed and this also makes the compiler aware that the property is non-nullable though it'll be initialized later hence reducing null checks on this property.

By Lazy()

Means lazy initialization

Lazy initialization simply works in the form of *I will only provide memory location for this value only when you use it in your code and I will only do this once and that's it, all your other usages will be fetched from cache *

 // waste of memory if this variable is not going to be used
private val pi : float = 3.142f

introducing lazy

private val pi : float by lazy{
3.142f
}

fun main(args:Array<String>){
 var area = pi * 7
}

This simply means the value 3.142f will not be allocated memory until its usage just to avoid unnecessary initialization of objects.

The variables can only be val and non-nullables.

Summary

When To Use Lateinit

  • Used to initialize a variable late.

  • Used when you are sure about initializing a variable before using it.

  • Used with the var keyword.

  • Used if variables will change at a later stage, i.e., if the variable is mutable.

When to use Lazy initialization

  • Variable will not be initialized unless it is called/used.

  • The Lazy initialization initializes the variable once; that same value is then used throughout the code as it is fetched from cache.

  • It is used for read-only properties as the same valued variable is used throughout.

  • This initialization is used in the case of the val property.

  • It can be used when an object is dependent on a variable internal to the class.

I think this much wraps up on the usages of lazy and lateinit in kotlin and my hope is that the basic explanation on the two concepts has helped you understand how they work and are now in a position to implement them on an actual code.

Remember to keep learning and keep growing every day.