How Kotlin Properties, Getters and Setters works
  • Posted by:

    Adhish L

    • May 09, 2019

    How Kotlin Properties, Getters and Setters works?

  • Blogs
  • How Kotlin Properties, Getters and Setters works?
  • Blogs
  • How Kotlin Properties, Getters and ...

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

 

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 as shown below:

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

 

 

how-kotlin-properties-getters-and-setters-works-inner-1

 

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

Property, Getter and Setter in Kotlin

Before we start, let's understand what is a property. A variable having a class level scope (member variables) which are declared inside the class but outside the methods or functions is called Property in Kotlin. They are declared with the 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 [: ] [= ]
[]
[]

 

 

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 does the work for you. It automatically infers and uses 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

 

 

how-kotlin-properties-getters-and-setters-works-inner-2

 

How it works internally?

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

The following code is 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 gets passed to the setter parameter value and sets the 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've finally managed to avoid the use of boilerplates in 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 coding.

how-kotlin-properties-getters-and-setters-works-inner-3

That was one example on how to write one line code and it automatically infers using POJO class and makes it ready to use. So basically, though we write just one line, internally the Kotlin compiler does its work and it understands the code. Isn't that awesome?

I have put my knowledge to explain how the Kotlin properties, getters and setters actually work, 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.

TOP