How Kotlin Properties, Getters and Setters works?

Introduction

When I started developing Android applications in Java, the encapsulation in the OOPs is achieved through making a POJO classes (or say, Getters and Setters) where the variables were declared as private and its getter and setter methods as public like:


class Person {
private String name;
private int age;

public String getName() {
return this.name;
}

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

public int getAge() {
return this.age;
}

public void setAge(String age) {
this.age = age;
}

}

But in Kotlin, this all can be done with a single line of code. Like shown below:


class Person{ var String: name, var Int: age }

 

This fascinated me towards Kotlin a lot, thinking that how a single line of code can replace the variable, getter and setter with same functionality! So, I deep dived into it to understand how basically the Kotlin works. Let’s get started!

 

Property, Getter and Setter in Kotlin

Before we start, let’s understand what is a property. Variable having a class level scope (member variables) which are declared inside the class but outside the methods or functions is called as Property in Kotlin. They are declared with var keyword for a mutable one and with val keyword for a read-only/non-mutable one.

The full syntax for declaring a property is

var < propertyName >[: < PropertyType > ] [= < property_initializer >]
[ < getter > ]
[ < setter > ]

But, the property_initializer, getter and setter are completely optional in Kotlin making it a beautiful language.

While using Kotlin, you get rid of writing or generating boilerplates. The compiler the work for you. It automatically infers and make you to use the variables directly as shown below:

//Setter
val person = Person()
person.name = “Adhish”
person.age = 18

//Getter
val name = nameDataClass.name
val age = nameDataClass.age

But how it internally works?

In Kotlin, the getters and setters are optional are and auto-inferred by the compiler even if you don’t write it in your code.

The following code in Kotlin:

class Person{ var String: name, var Int: age }

gets treated internally as:

class Person {

var name: String = ""
get() = field
set(value) {

field = value
}

var age: Int = 0
get() = field
set(value) {
field = value
}

}

So, when you instantiate the class Person and initialise the name or age property, it get passed to the setter parameter value and sets field to value as

set(value) {
field = value
}

Similarly, when you try to access name or age property from the object, you will get the value set or the default value because

get() = field

Or you have already initialised a default value as “” for the name and 0 for the age

Conclusion

With the introduction of Kotlin language, we finally managed to avoid much of the boilerplates from our codes which in return saves our time and makes our code much cleaner. The Kotlin compiler is designed in such a way that it uses AI and infers much of the thing. We just saw one example above that how we write just one line of code and it automatically infers that it is a POJO class and makes it ready to use. So basically, though we write just one line, but internally the Kotlin compiler does its work and it understands the code.

Kotlin is not different or tough to understand, it is more or less the same with little bit of syntax changes(of-course in the easier way).

I have tried my best to explain that how the Kotlin properties, getters and setters actually works, if you still have any doubts, please feel free to write to me. Also, buzz me up if you want me to write about some other Kotlin Android topics.