codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

21 / 68 topics
11Extensions Functions12Classes and Objects13Inheritance in Kotlin14Interfaces in Kotlin15Data Classes16Sealed Classes17Companion Objects18Generics in Kotlin19Type Aliases20Properties and Fields21Lateinit Properties22Delegated Properties
Tutorials/Kotlin/Lateinit Properties
🎯Kotlin

Lateinit Properties

Updated 2026-04-20
3 min read

Lateinit Properties

Introduction

In Kotlin, properties can be declared with a lateinit modifier. This feature is particularly useful when you need to initialize a property at a later point in the lifecycle of an object, such as after dependency injection or during configuration. In this tutorial, we will explore how to use lateinit, its constraints, and best practices for its application.

What are Lateinit Properties?

A lateinit property is a non-null property that can be initialized at a later time than usual. Normally, Kotlin requires properties to be initialized at the point of declaration or in the constructor. However, with lateinit, you can declare a property without initializing it immediately and initialize it later.

Key Characteristics

  • Non-null: The property must have a non-null type.
  • Mutable: The property must be var (mutable).
  • Class-level: Can only be used on class properties, not local variables or top-level properties.
  • Backing Field: Kotlin generates a backing field for the property, allowing it to be initialized later.

Declaring Lateinit Properties

To declare a lateinit property, use the lateinit modifier before the property declaration. Here is an example:

class User {
    lateinit var name: String

    fun initializeName(name: String) {
        this.name = name
    }

    fun printName() {
        println("User's name is $name")
    }
}

In this example, the name property is declared as lateinit. It can be initialized later using the initializeName method.

Initializing Lateinit Properties

You can initialize a lateinit property at any point after its declaration. However, you must ensure that it is initialized before accessing it, otherwise, an UninitializedPropertyAccessException will be thrown.

fun main() {
    val user = User()
    user.initializeName("John Doe")
    user.printName()  // Output: User's name is John Doe
}

In this example, the name property is initialized after creating an instance of User.

Accessing Lateinit Properties

Once a lateinit property is initialized, you can access it like any other non-null property.

fun main() {
    val user = User()
    user.initializeName("Jane Doe")
    println(user.name)  // Output: Jane Doe
}

Constraints and Limitations

While lateinit properties are powerful, they come with certain constraints:

  • Non-primitive Types: Only non-primitive types (classes, interfaces, etc.) can be declared as lateinit. Primitive types like Int, Double, etc., cannot use this modifier.

    // This will cause a compilation error
    class Example {
        lateinit var number: Int  // Error: 'lateinit' modifier is not allowed on properties of primitive types
    }
    
  • Initialization Check: You can check if a lateinit property has been initialized using the isInitialized extension property.

    fun main() {
        val user = User()
        println(user::name.isInitialized)  // Output: false
        user.initializeName("Alice")
        println(user::name.isInitialized)  // Output: true
    }
    
  • No Default Value: Unlike regular properties, lateinit properties do not have default values.

Best Practices

Use When Necessary

Only use lateinit when you are sure that the property will be initialized before it is accessed. Overusing lateinit can lead to runtime errors and make your code harder to debug.

Initialize Early

Initialize lateinit properties as early as possible in the object's lifecycle to minimize the risk of uninitialized access.

Use isInitialized for Safety

Always check if a lateinit property is initialized before accessing it, especially in complex applications where initialization might not be straightforward.

fun printUserName(user: User) {
    if (::name.isInitialized) {
        println("User's name is ${user.name}")
    } else {
        println("Name has not been initialized")
    }
}

Avoid Overuse

Avoid using lateinit for properties that can be initialized at the point of declaration. This keeps your code more predictable and easier to understand.

Real-World Example: Dependency Injection

In a dependency injection framework, you might need to inject dependencies after an object has been instantiated. lateinit is perfect for this scenario.

class DatabaseConnection {
    lateinit var url: String
    lateinit var username: String
    lateinit var password: String

    fun connect() {
        // Connect to the database using url, username, and password
    }
}

fun configureDatabase(connection: DatabaseConnection) {
    connection.url = "jdbc:mysql://localhost:3306/mydb"
    connection.username = "admin"
    connection.password = "password"
    connection.connect()
}

In this example, url, username, and password are initialized after the DatabaseConnection object is created.

Conclusion

lateinit properties in Kotlin provide a flexible way to initialize properties at a later time. While they offer significant benefits, especially in complex applications, it's important to use them judiciously and follow best practices to maintain code clarity and reliability. By understanding when and how to use lateinit, you can write more robust and maintainable Kotlin applications.


This comprehensive guide should provide you with a solid understanding of lateinit properties in Kotlin, including their declaration, initialization, constraints, and practical applications.


PreviousProperties and FieldsNext Delegated Properties

Recommended Gear

Properties and FieldsDelegated Properties