Open In App

Kotlin Class Properties and Custom Accessors

Last Updated : 01 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In object-oriented programming, encapsulation is one of the most fundamental principles. It refers to bundling data (fields) and the code that operates on that data (methods) into a single unit - the class. Kotlin takes this principle even further with properties, a feature that replaces traditional Java style fields and accessor methods.

What is Encapsulation?

In Java, data in a class is typically stored in private fields, and accessor methods (getters and setters) are used to read or modify these values. Setters may also include validation logic or notify listeners of changes.

Example:

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

What is a Property in Kotlin?

It is the combination of accessories and the fields in the case of Java. In the case of Kotlin, properties are meant to be a first-class language feature. These features replace fields and accessor methods. A property in a class is declared the same as declaring a variable with val and var keywords. A property declared as var is mutable and thus, can be changed.

Defining a class:

Kotlin
class Abc(
    val name: String, 
    val ispassed : Boolean
)
  • val: Read-only property (generates only a getter)
  • var: Read-write property (generates a getter and setter)

Basically what happens is that the declaration of the property declares the related accessors (both setter and getter for writable and getter for readable property). A field stores the value.

Let's see the class usage 

Kotlin
class Abc(
    val name: String,
    val ispassed : Boolean
)

fun main(args: Array<String>) {

    val abc = Abc("Bob",true)
    println(abc.name)
    println(abc.ispassed)

    /*
    In Java
    Abc abc = new Abc("Bob",true);
    System.out.println(person.getName());
    System.out.println(person.isMarried());

    */
}

Output: 

Bob
true

In Kotlin, the constructor can be called without a new keyword. Instead of invoking the getter, the property is referenced directly. The logic remains the same and the code is much more concise. Setters of mutable property works in a similar manner.  

Custom Accessors (Getter and Setter)

We can define our own logic in the getter and setter of a property. This is called a custom accessor.  

Example of Custom Getter:

Kotlin
class Rectangle(val width: Int, val height: Int) {
    val isSquare: Boolean
        get() = width == height
}

fun main() {
    val rect = Rectangle(10, 20)
    println(rect.isSquare)
}

Output: 

false

The property isSquare needs no field to store the value. It only has a custom getter with the implementation provided. Every time the property is accessed, the value is computed.

Example of Custom Setter:

Kotlin
class Person {
    var age: Int = 0
        set(value) {
            if (value > 0) field = value
            else println("Age must be positive")
        }
}

fun main() {
    val p = Person()
    p.age = -5
    p.age = 30
    println(p.age)
}

Output:

Age must be positive
30

In this case, the custom setter validates the value before updating the backing field.

Backing Field (field)

When using custom accessors, we often see the field keyword. This represents the actual storage behind the property. The keyword field can only be accessed within custom get() and set() methods. If your property does not store a value (like isSquare - as shown in the previous example of custom getter), you don't need a field.


Next Article
Article Tags :

Similar Reads