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

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

Properties and Fields

Updated 2026-04-20
3 min read

Properties and Fields

Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM) and also compiles to JavaScript source code. One of its core features is the ability to define properties, which are used to store data within classes or objects. In this tutorial, we will explore properties and fields in Kotlin, including how they differ, when to use each, and best practices for defining them.

Understanding Properties

In Kotlin, a property is a member of a class that holds some value. It can be declared with the var keyword (mutable) or the val keyword (immutable). Properties are defined using the following syntax:

class Person {
    var name: String = "John"
    val age: Int = 30
}

In this example, name is a mutable property that can be changed after initialization, while age is an immutable property that cannot be changed once it's set.

Property Initialization

Properties in Kotlin must be initialized before they are used. This can be done directly at the point of declaration or through a constructor:

class Person {
    var name: String = "John"
    val age: Int

    constructor(age: Int) {
        this.age = age
    }
}

Alternatively, you can use property initializers for more complex initialization logic:

class Person {
    var name: String = "John"
    val age: Int

    init {
        age = 30 // Initialization block
    }
}

Fields

In Kotlin, fields are not explicitly declared like in Java. Instead, the compiler automatically generates backing fields for properties when needed. A backing field is a storage location where the value of a property is stored.

Backing Fields

When you access a property, Kotlin uses a getter and setter to read and write its value. If you need to customize these behaviors, you can define your own getters and setters:

class Person {
    var name: String = "John"
        get() = field.toUpperCase()
        set(value) {
            if (value.isNotBlank()) field = value
        }
}

In this example, the name property has a custom getter that converts the stored value to uppercase and a setter that only updates the value if it's not blank.

Customizing Accessors

Kotlin allows you to customize accessors for properties. You can define different visibility levels for getters and setters:

class Person {
    var name: String = "John"
        private set // The setter is private, but the getter remains public
}

This makes the name property readable from outside the class but only writable within the class.

Property Delegation

Property delegation is a powerful feature in Kotlin that allows you to delegate the responsibility of storing and managing a property's value to another object. This is useful for implementing lazy initialization, observable properties, or other complex behaviors:

class Person {
    var name: String by Delegates.observable("John") { _, oldValue, newValue ->
        println("Name changed from $oldValue to $newValue")
    }
}

In this example, the name property uses a delegate that prints a message whenever its value changes.

Best Practices

  1. Use Immutable Properties When Possible: Prefer using val over var when you don't need to change the property's value after initialization. This makes your code safer and easier to understand.
  2. Keep Properties Private by Default: If a property is only used within its class, declare it as private. This encapsulates the implementation details and prevents external access.
  3. Use Property Delegation for Complex Behaviors: When you need to implement complex behaviors like lazy initialization or validation, use property delegation instead of writing boilerplate code.
  4. Customize Accessors Carefully: Only customize getters and setters when necessary. Overuse can make your code harder to read and maintain.

Conclusion

Properties and fields are fundamental concepts in Kotlin that allow you to define and manage data within classes. By understanding the differences between properties and fields, customizing accessors, and using property delegation, you can write more efficient, readable, and maintainable Kotlin code.


PreviousType AliasesNext Lateinit Properties

Recommended Gear

Type AliasesLateinit Properties