June 2018
Intermediate to advanced
316 pages
6h 34m
English
The lateinit modifier allows you to declare non-nullable variables without initializing them. Let's look at the following example:
class Main { private lateinit var name: String fun onCreate() { name = "Jack" println(name) }}
This code, when decompiled to Java, looks as follows:
public final class Main { private String name; public final void onCreate() { this.name = "Jack"; String var10000 = this.name; if (this.name == null) { Intrinsics.throwUninitializedPropertyAccessException("name"); } String var1 = var10000; System.out.println(var1); }}
Whenever you try to get a value from the name property, an additional check is invoked:
if (this.name == null) { Intrinsics.throwUninitializedPropertyAccessException("name");}
Read now
Unlock full access