# Default getter/setter versus normal function in Kotlin

**URL:** <https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304>\
**Category:** Language Design\
**Created:** [February 22, 2017, 5:54pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304 "2017-02-22T17:54:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 22, 2017, 5:54pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/1 "2017-02-22T17:54:59Z")

</div>

Hi, I am very new to Kotlin, probably two weeks ago, I would not even have guessed myself developing softwares other than in Java. Guessed now who I am?

Here is my question.

public class Hello {

```
val name: String
    get() = "Hi"

fun getName():String {
    return "Hi"
}

```

}

I know this will give you compile time error. But I would be happy to have a function getName() that looks easy to invoke than property ‘name’ which has custom getter like the above. Please help me understand why i should go for custom getters and setters, and when i would be using hard-core functions.

Thanks,  
Abu A

---

<div class="post-metadata">

**Author:** ![alanfo](https://avatars.discourse-cdn.com/v4/letter/a/bbe5ce/32.png) [@alanfo](https://discuss.kotlinlang.org/u/alanfo)\
**Post date:** [February 23, 2017, 12:14am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/2 "2017-02-23T00:14:26Z")

</div>

There is a section at the end of the Kotlin Coding Conventions which says this:

"In some cases functions with no arguments might be interchangeable with read-only properties. Although the semantics are similar, there are some stylistic conventions on when to prefer one to another.

Prefer a property over a function when the underlying algorithm:

- does not throw
- has a O(1) complexity
- is cheap to calculate (or caсhed on the first run)
- returns the same result over invocations"

So, the answer here is that you should prefer the `name` property to a `getName()` method.

Incidentally, you could simplify the property to just this:

```nohighlight
public class Hello {
    val name = "Hi"
}

```

This initializes it, infers its type to be String and creates a default getter.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [February 23, 2017, 7:11am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/3 "2017-02-23T07:11:33Z")

</div>

> [@alanfo](#):
>
> ```
> val name = "Hi"
> 
> ```

This version creates instance attribute.

> [@abuabdul](#):
>
> ```
> val name: String
> get() = "Hi"
> 
> ```

This version creates only getter.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 23, 2017, 9:03am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/4 "2017-02-23T09:03:01Z")

</div>

But both will be invoked in the same manner, i.e Hello().name right?

Looking outward when there is no access to the source code, developers may not know whether its just a instance attribute or just getter. Is there any way we can find it if the source is not open?

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 23, 2017, 9:05am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/5 "2017-02-23T09:05:26Z")

</div>

The algorithm seems to be same for all languages. When its cheap to calculate or same result over multiple invocations, all language prefers to property I guess. Thanks @alanfo

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [February 23, 2017, 9:38am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/6 "2017-02-23T09:38:28Z")

</div>

> [@abuabdul](#):
>
> But both will be invoked in the same manner, i.e Hello().name right?

Yes, this doesn’t change the public interface.

> [@abuabdul](#):
>
> Is there any way we can find it if the source is not open?

Reflection, class decompilation or checking memory footprint.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 23, 2017, 11:08am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/7 "2017-02-23T11:08:53Z")

</div>

Thanks @fvasco

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [February 23, 2017, 1:59pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/8 "2017-02-23T13:59:18Z")

</div>

Also remember that the JVM applies various optimizations. For even the most minimally optimized code it is likely that any trivial getters (those that only return a field) will be inlined to direct field accesses. As such you would not choose a direct field access just for optimization purposes as the JVM will happily do so for you, but without having to worry about binary compatibility if you choose to have a non-trivial getter in the future.

Kotlin properties are designed with this in mind and will (for non-private properties) always create getters and setters. Optimization is left to the JVM that will do this anyway.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [February 23, 2017, 3:08pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/9 "2017-02-23T15:08:36Z")

</div>

> [@pdvrieze](#):
>
> Optimization is left to the JVM that will do this anyway.

You have right, but define an instance attribute _may_ have memory impact (it depends by padding).

For a constant getter doesn’t declare attribute _can_ have a memory benefit (no allocation/initialization), in other case save a computed value in an instance attribute may lower cpu load.

In any way “fun getName” isn’t the right way, others solution may become only a premature optimization.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 24, 2017, 9:46am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/10 "2017-02-24T09:46:06Z")

</div>

I am starting to feel Kotlin has some restrictive style in using the properties and its getters and setters in the sense that both are closely tied up.

private var name: String

I can not have public custom getter for this. If i want to declare a fun that returns this property, i must name something else; getName() clashes with get-name() (jvm thing). Or I must explicitly define this variable as public.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [February 24, 2017, 10:00am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/11 "2017-02-24T10:00:28Z")

</div>

If you want to have a property which can be accessed outside of a class but modified only inside the class, you can do this by defining a custom visibility for the setter:

```
var name: String
   private set

```

You’re never supposed to write functions that return values of properties; if you feel the need to do that, then most likely you aren’t using properties correctly.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 24, 2017, 11:03am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/12 "2017-02-24T11:03:33Z")

</div>

There are many Java Frameworks that needs class that hides the property and opens up public accessors for the property, a typical POJO. Wouldnt be needed in Kotlin as such?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [February 24, 2017, 11:18am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/13 "2017-02-24T11:18:22Z")

</div>

A regular property in Kotlin is compiled to a private field and public accessor methods. This is exactly what the Java frameworks need.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 24, 2017, 11:29am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/14 "2017-02-24T11:29:06Z")

</div>

var name:String = “Hi”

This will be treated as field with public accessors by Kotlin? I am curious about the regular property modifier in this case.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [February 24, 2017, 11:42am UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/15 "2017-02-24T11:42:42Z")

</div>

This Kotlin property declaration is equivalent to the following Java code:

```
 private String name;

 public String getName() { return name; }

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

```

Not sure what you mean by “the regular property modifier”.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 24, 2017, 12:36pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/16 "2017-02-24T12:36:01Z")

</div>

Ok I think public modifier is default in the declaration:

var name:String = “Hi”

Hence i thought this would be converted like this in Java Equivalent. (Note the public modifier in the java property)

```
 public String name;

 public String getName() { return name; }

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

```

So i explicitly put private before the declaration. Are you trying to explain?

`private var name:String = "Hi"`

will be converted to this below.

```
private String name;

public String getName() { return name; }

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

```

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [February 24, 2017, 12:45pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/17 "2017-02-24T12:45:54Z")

</div>

The backing field of a property is always private. The visibility modifier of a property determines the visibility of its accessor methods. If you have a `private var name`, the `getName()` and `setName()` methods will also be private.

---

<div class="post-metadata">

**Author:** ![abuabdul](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abuabdul/32/2355_2.png) [@abuabdul](https://discuss.kotlinlang.org/u/abuabdul)\
**Post date:** [February 24, 2017, 12:56pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/18 "2017-02-24T12:56:38Z")

</div>

I see. It makes sense now. Thanks for your time. @yole

_I think i am gonna love Kotlin_

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [February 24, 2017, 3:30pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/19 "2017-02-24T15:30:34Z")

</div>

> [@fvasco](#):
>
> You have right, but define an instance attribute may have memory impact (it depends by padding).
> 
> For a constant getter doesn’t declare attribute can have a memory benefit (no allocation/initialization), in other case save a computed value in an instance attribute may lower cpu load.

If the return value doesn’t change the value can be stored as a static field (or field a companion in Kotlin). Of course if the value is a string or primitive it will be able to live in the constant pool and a static field is a repetition.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [March 1, 2017, 9:01pm UTC](https://discuss.kotlinlang.org/t/default-getter-setter-versus-normal-function-in-kotlin/2304/20 "2017-03-01T21:01:14Z")

</div>

> [@abuabdul](#):
>
> val name: String  
> get() = “Hi”

Now that 1.1 had been released there are some changes affecting this.

First in a case like this types can be inferred in 1.1:

```
 val name get() = "Hi"

```

Also property accesors in 1.1 can be inline. You can declare it on the accessor or the whole property:

```
inline val name get() = "Hi"

```

I would also ask if this is a constant value would you not want a const val (which would go at top level or in a companion object):

```
const val name = "Hi"

```

If you did go this route in 1.1 this will now be inlined
