Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getters and Setters in Kotlin

In Java, for example, I can write getters on my own (generated by IDE) or use Annotations like @Getter in lombok - which was pretty simple.

Kotlin however has getters and setters by default. But I can't understand how to use them.

I want to make it, lets say - similar to Java:

private val isEmpty: String         get() = this.toString() //making this thing public rises an error: Getter visibility must be the same as property visibility. 

So how do getters work?

like image 340
nutella_eater Avatar asked Jun 19 '16 11:06

nutella_eater


People also ask

What are Kotlin getters and setters?

In Kotlin, setter is used to set the value of any variable and getter is used to get the value. Getters and Setters are auto-generated in the code. Let's define a property 'name', in a class, 'Company'. The data type of 'name' is String and we shall initialize it with some default value.

How do you get a getter setter in Kotlin?

There is no "standard getter and setter" in Kotlin. Or, rather, they are built into the language. Your get() and set() are overriding the "standard getter and setter". In Kotlin, fields are already translated into standard getters and setters.

What is getter and setter in Android?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value.

What is private VAR in Kotlin?

In Kotlin, private modifiers allow only the code declared inside the same scope, access. It does not allow access to the modifier variable or function outside the scope.


2 Answers

Getters and setters are auto-generated in Kotlin. If you write:

val isEmpty: Boolean 

It is equal to the following Java code:

private final Boolean isEmpty;  public Boolean isEmpty() {     return isEmpty; } 

In your case the private access modifier is redundant - isEmpty is private by default and can be accessed only by a getter. When you try to get your object's isEmpty property you call the get method in real. For more understanding of getters/setters in Kotlin: the two code samples below are equal:

var someProperty: String = "defaultValue" 

and

var someProperty: String = "defaultValue"     get() = field     set(value) { field = value } 

Also I want to point out that this in a getter is not your property - it's the class instance. If you want to get access to the field's value in a getter or setter you can use the reserved word field for it:

val isEmpty: Boolean   get() = field 

If you only want to have a get method in public access - you can write this code:

var isEmpty: Boolean     private set  

due to the private modifier near the set accessor you can set this value only in methods inside your object.

like image 133
Cortwave Avatar answered Oct 19 '22 01:10

Cortwave


The rules about property accessors visibility modifiers are the following:

  • Getter visibility of var and val property should be exactly the same to the visibility of the property, thus you can only explicitly duplicate the property modifier, but it is redundant:

    protected val x: Int     protected get() = 0 // No need in `protected` here. 
  • Setter visibility of var property should be the same or less permissive than the property visibility:

    protected var x: Int     get() = 0     private set(x: Int) { } // Only `private` and `protected` are allowed. 

In Kotlin, properties are always accessed through getter and setter, thus there's no need in making a property private with public accessors like in Java -- its backing field (if present) is already private. So, visibility modifiers on property accessors are only used to make setter visibility less permissive:

  • For a property with backing field and default accessors:

    var x = 0 // `public` by default     private set 
  • For a property without backing field:

    var x: Int // `public` by default     get() = 0     protected set(value: Int) { } 
like image 32
hotkey Avatar answered Oct 19 '22 00:10

hotkey