Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - How to decide between "lateinit" and "nullable variable"?

Tags:

android

kotlin

I am confuse for lateinit and nullable variable, which one to use for variable.

lateinit var c: String var d: String? = null c = "UserDefinedTarget"  // if not added initialisation for c than throws UninitializedPropertyAccessException if (c == "UserDefinedTarget") {     //do some stuff. } //not throws any exception whether d is initialise or not. if(d == "UserDefinedTarget") {      //do some stuff } 
like image 237
paril Avatar asked Jun 28 '17 07:06

paril


People also ask

When should I use Lateinit?

lateinit means that variable must be initialised later. It should be initialized before accessing it. If you attempt accessing uninitialized lateinit variable UninitializedPropertyAccessException will be thrown. It's always better to avoid using nulls in your app.

What is Lateinit in Kotlin and when would you use it?

Android Online Course for Professionals by MindOrks Lateinit is allowed for non-primitive data types only and the variable can't be of null type. Also, lateinit variable can be declared either inside the class or it can be a top-level property.

How do I know if Lateinit is initialized?

You can check if the lateinit variable has been initialized or not before using it with the help of isInitialized() method. This method will return true if the lateinit property has been initialized otherwise it will return false.


2 Answers

A type that is nullable is just that, a thing that has a valid state that is null.

A non-nullable late init var represents something where null is an invalid state, but for some reason you can't populate it in the constructor.

Android Activities are a good example of a use of lateinit. Activities must have a no args constructor and their lifecycle only really starts with onCreate().

like image 131
alex Avatar answered Sep 22 '22 02:09

alex


These are two completely different concepts.

You can use lateinit to avoid null checks when referencing the property. It's very convenient in case your properties are initialized through dependency injection, or, for example, in the setup method of a unit test.

However, you should keep in mind that accessing a lateinit property before it has been initialized throws an exception. That means you should use them only if you are absolutely sure, they will be initialized.

Nullable types, on the other hand, are used when a variable can hold null.


class A {     lateinit var a: String      fun cat() {         print(a.length)  // UninitializedPropertyAccessException is thrown         a = "cat"         print(a.length)  // >>> 3     } }  class B {     var b: String? = null      fun dog() {         print(b.length)  // won't compile, null check is obligatory here         print(b?.length) // >>> null         b = "dog"         print(b?.length) // >>> 3     } } 

For more information:

  • Late-initialized properties
  • Nullable types
like image 31
Alexander Romanov Avatar answered Sep 25 '22 02:09

Alexander Romanov