lateinit in Kotlin: How to Check if a Variable Has Been Initialized

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why lateinit is Both Convenient and Dangerous

\n\n

If you've been writing Kotlin for at least a few weeks, you've likely encountered this situation: you need to declare a variable that definitely won't be null, but you can't initialize it right away. For example, when data comes from an external API, user input, or after loading a configuration. In Java, we would simply write String name = null; and forget about it. But Kotlin is a language with strict null safety, and you can't just assign null to a variable.

\n\n

This is where the lateinit modifier comes into play. It allows you to declare a non-null variable without immediate initialization, postponing it for later. Sounds ideal, right? But there's a catch: if you try to access such a variable before it has been assigned a value, Kotlin will throw an UninitializedPropertyAccessException. And your application will crash.

\n\n

This is precisely where a popular Stack Overflow question (with thousands of views) arises: “How to check if a lateinit variable has been initialized?”. In this article, we won't just give you the answer — we'll break down how lateinit works, show you how to safely check the variable's state, and explain when lateinit is the right choice and when you should use other approaches.

\n\n

What is lateinit and Why Do You Need It?

\n\n

The lateinit modifier is a promise to the compiler: “I know this variable is currently empty, but I guarantee I will fill it before the first use.” Kotlin trusts you and doesn't require assigning a value right at the declaration point.

\n\n

Key characteristics of lateinit:

\n
    \n
  • Can only be applied to var (mutable properties), not val.
  • \n
  • Works only with reference types (classes, strings, collections). Primitives (Int, Double, etc.) are not supported.
  • \n
  • Cannot be used with nullable types (with ?).
  • \n
  • Does not support custom getters and setters.
  • \n
\n\n

A classic example is Android development, where objects are often initialized in onCreate() or onViewCreated():

\n\n
class MainActivity : AppCompatActivity() {\n    lateinit var userAdapter: UserAdapter\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        // Here we must initialize userAdapter\n        userAdapter = UserAdapter(emptyList())\n    }\n\n    fun updateAdapter(newList: List<User>) {\n        // Safe because we know onCreate has already been executed\n        userAdapter.updateData(newList)\n    }\n}\n
\n\n

But what if someone calls updateAdapter() before onCreate()? That's right — a crash. And this is exactly where checking for initialization comes in handy.

\n\n

How to Check if a lateinit Variable Has Been Initialized?

\n\n

Kotlin provides a built-in property isInitialized, accessible via reflection for any lateinit property. The syntax is simple: ::propertyName.isInitialized. Note the double colon — this is the property reference operator.

\n\n

Here's a basic example:

\n\n
class DataManager {\n    lateinit var config: Config\n\n    fun loadConfig() {\n        // Simulating configuration loading\n        config = Config("production", 8080)\n    }\n\n    fun printConfig() {\n        if (::config.isInitialized) {\n            println("Configuration: ${config.mode}, port: ${config.port}")\n        } else {\n            println("Configuration not loaded yet!")\n        }\n    }\n}\n\nfun main() {\n    val manager = DataManager()\n    manager.printConfig() // Output: Configuration not loaded yet!\n    manager.loadConfig()\n    manager.printConfig() // Output: Configuration: production, port:                

Blogs

Book Recommendations