Skip to content
DeveloperMemos

Understanding Kotlin's lateinit Modifier

Kotlin, lateinit modifier2 min read

Kotlin is a programming language that has gained a lot of popularity recently due to its modern syntax, interoperability with Java, and support for functional programming. One of the unique features of Kotlin is the lateinit modifier. In this post, we’ll explore what lateinit is, when to use it, and how it can be used in Android app development.

What is lateinit?

In Kotlin, variables must be initialized before they are used. However, sometimes it may not be possible to initialize a variable during instantiation, such as when dealing with dependency injection or lazy initialization. This is where the lateinit modifier comes into play. It allows us to declare a variable without initializing it immediately and postpone its initialization until later.

The lateinit modifier can only be used with mutable data types, i.e., those that can be modified after initialization, such as var properties. Additionally, the lateinit modifier cannot be used with nullable types or primitive data types, as they need to be initialized at the time of declaration.

When to use lateinit?

We should only use lateinit when we are sure that the variable will be initialized before it is accessed. Otherwise, a lateinit property access before initialization will throw a NullPointerException at runtime. So, we need to make sure that the initialization happens before accessing the lateinit property to avoid any exceptions.

Example Usage in Android App Development

In Android app development, we can use lateinit modifier for variables that require initialization after the onCreate() method of the Activity or Fragment has been called. For instance, consider the following example:

1class ExampleActivity : AppCompatActivity() {
2
3 private lateinit var exampleTextView: TextView
4
5 override fun onCreate(savedInstanceState: Bundle?) {
6 super.onCreate(savedInstanceState)
7 setContentView(R.layout.activity_example)
8
9 exampleTextView = findViewById(R.id.example_text_view)
10 exampleTextView.text = "Hello World"
11 }
12}

Here, we have declared a lateinit property named exampleTextView of type TextView. We initialize it in the onCreate() method by calling findViewById() to find a TextView with ID R.id.example_text_view from the layout file and set its text to “Hello World”. By using lateinit, we are delaying the initialization of the exampleTextView variable until after the onCreate() method is called.

Another example usage is with dependency injection frameworks like Dagger, where we may not have access to an object's constructor and need to delay its initialization until later.

1class ExampleFragment : Fragment() {
2
3 @Inject
4 private lateinit var exampleDependency: ExampleDependency
5
6 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
7 super.onViewCreated(view, savedInstanceState)
8
9 // use exampleDependency here
10 }
11}

Here, we have declared a lateinit property named exampleDependency of type ExampleDependency. We annotate it with @Inject to tell Dagger to inject it when the Fragment is created. The actual initialization happens during runtime, which is why we use lateinit.

Wrapping Up

In conclusion, lateinit is a powerful modifier in Kotlin that allows us to delay the initialization of a variable until later. We should only use lateinit when we are sure that the variable will be initialized before it is accessed, and it cannot be used with nullable or primitive data types. In Android app development, we can use lateinit modifier for variables that require initialization after the onCreate() method has been called or with dependency injection frameworks like Dagger.