Property, Getter, and Setter — Kotlin

Property, Getter, and Setter  - Kotlin

We all are Android developers and we all started developing Android applications in Java by using OOPs concepts. But after the introduction of the Kotlin language, we all started moving towards the Kotlin language because it is now the recommended language for Android app development and during this transformation phase, we all noticed lots of differences in between these languages. Like in Java, we declare variables as private and after declaring that, we make one getter and one setter method for the same variable and these methods are made public. But when we moved to Kotlin, the whole process get reduced to just one line of code.

Property, Getter, and Setter  - Kotlin

In this blog, we will learn about Property, Getter and Setter in Kotlin. But before starting the blog, have a smile on your face not because you have to write only one line of code for Getters and Setters in Kotlin, but because you are going to learn something new today :)

Properties

Properties are the variables (to be more precise, member variables) that are declared inside a class but outside the method. Kotlin properties can be declared either as mutable using the “var” keyword or as immutable using the “val” keyword.

Following is the syntax of property declaration:

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

Here, property initializer, getter and setter are optional. Following is an example of the same:

fun main(args : Array) {
    val name: String = "MindOrks"
    var members: Int = 5000
    members = 7000 // In can be assigned any number of times
    name = "New Name" // It can not be assigned again
}

By default, all properties and functions in Kotlin are public . But in case of private and protected , you have to add it explicitly. Also, all properties in Kotlin are final by default. But what if I want to declare a variable as private and then add its getter and setter as public? Let’s see.

Getter and Setter

As the name suggests, Getters are used to get the value of variables and Setters are used to set the value of any variable. No clapping please :) Let’s have an example. Following is the Java code for Getters and Setters:

class Community {
 
    //private variable
    private String name;

    //getter for name
    public getName() {
        return name;
    }
    
    //setter for name
    public setName(String name) {
        this.name = name;
    }
    
}

Here, we are having two private variables named name and roll . Apart from this, we have Getters and Setters for these variables also. If we write the same code in Kotlin, then the code will be:

class Community {
    var name: String = "MindOrks"
}

That’s it. Trust me! This is the code for getters and setters in Kotlin. Getters and setters are auto-generated in the code. So, they are automatically created when we declare properties. The equivalent code for the above Kotlin code for Getter and Setter can be written as:

class Community {
    var name: String = "MindOrks"
        get() = field                     // getter
        set(value) { field = value }      // setter
}

So, why so write 2-3 lines of code when you can do the same just in one line?

Let’s look at some more examples. In the above class i.e. in the Community class, add two more private variables.

class Community {

    //private variable
    private String name;
    private String startingDate;
    private String desc; //description of community

    //getter for desc
    public getDesc() {
        return name + " " + startingDate;
    }

    //setter for name
    public setDesc(String desc) {
        String descArray[] = desc.split(" ");
        name = descArray[0];
        startingDate = descArray[1];
    }

}

In the above code, the getter method returns the description of community with space and the setter is used to break the description into two parts i.e. the name of community and the starting date of the community. The equivalent Kotlin code will be:

private lateinit var name: String
private lateinit var startingDate: String
var desc: String
    get() = name + " " + startingDate
    set(value) {
        val descArray = value.split(" ".toRegex())
        name = descArray[0]
        startingDate = descArray[1]
    }

Isn’t it easy in Kotlin?

We can override the getter or setter in an extending class by defining the variable as open in the base class and then use the override keyword in the extending class.

//Base class
open var money: Int = 0
    get() = 10000

//Extending class
override var money: Int = 0
    get()= 20000

So, we started with a smile :) and ended with double smile :) :) . Hope you learned something new today.

Do share this blog with your fellow developers to spread the knowledge. You can read more blogs on Android on our blogging website .

Apply Now: MindOrks Android Online Course and Learn Advanced Android

Happy Learning :)

Team MindOrks!