In the Kotlin language we, by default, have to initialize each variable when it is introduced. To avoid this, the lateinit
keyword can be used. Referring to a lateinit
variable before it has been initialized results in a runtime exception.
lateinit
can not, however, be used with the primitive types. Why is it so?
Kotlin doesn't have primitive type (I mean you cannot declare primitive directly). It uses classes like Int , Float as an object wrapper for primitives. When kotlin code is converted to jvm code, whenever possible, "primitive object" is converted to java primitive.
In order to create a "lateInit" variable, we just need to add the keyword "lateInit" as an access modifier of that variable. Following are a set of conditions that need to be followed in order to use "lateInit" in Kotlin. Use "lateInit" with a mutable variable. That means, we need to use "var" keyword with "lateInit".
In the above code, you can see that the object of the HeavyClass is created only when it is accessed and also the same object is there all over the main() function. Lazy is mainly used when you want to access some read-only property because the same object is accessed throughout.
lateinit keyword exists to enable one specific scenario: when your field can't be null, but you also can't initialize it in the constructor or with a constant value. It is on you to make sure that you initialize the value before using it.
A short answer is that with primitives you can always use 0
as the default, and with nullable types null
as a default. Only non-nullable non-primitive types may need lateinit
to work around the type safety system.
Actually, there is no need for initializing a variable in Kotlin as long as it has a value before the first access and it can be statically proved. Which means this code is perfectly valid:
fun main(args: Array<String>) { var x: Int val y: Double x = 0 y = x + 0.1 println("$x, $y") }
But there are (rare) cases when the initialisation cannot be statically proved. The most common case is a class field which uses any form of dependency injection:
class Window { @Inject lateinit parent: Parent }
For (non-nullable) object types, Kotlin uses the null
value to mark that a lateinit
property has not been initialized and to throw the appropriate exception when the property is accessed.
For primitive types, there is no such value, so there is no way to mark a property as non-initialized and to provide the diagnostics that lateinit
needs to provide. (We could try to use a separate marker of some kind, but that marker would not be updated when initializing the field through reflection, which is a major use case of lateinit
).
Therefore, lateinit
is supported for properties of object types only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With