avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1245

Abstract

ir support.</p></blockquote><blockquote id="757e"><p>It is recommended to read the <a href="https://readmedium.com/introduction-2cc7eba631e2">Introduction</a> before moving on. Check out the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a> for all articles.</p></blockquote><p id="d7c0">When not specified, default getters and setters (i.e. <code>return field</code> and <code>field = value</code>) are automatically generated by the compiler. Kotlin also adds syntactic sugar that allows working with properties as if they were fields, so <code>obj.x</code> is actually <code>obj.getX()</code> and <code>obj.x = y</code> is actually <code>obj.setX(y)</code>. It is important to remember that <b>this is only syntactic sugar</b>, and it is the accessor functions that actually get called.</p><p id="4949">If you need to customize the accessors, use <code>get()</code> and/or <code>set()</code>:</p> <figure id="0e05"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FN00a8pi7n&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%

Options

2Fpl.kotl.in%2FN00a8pi7n&image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="f3c4">Equipped with this knowledge, we can state the technical difference between properties defined using <code>val</code> or <code>var</code>: <code>val</code> doesn't generate a setter, while <code>var</code> does.</p><p id="e89a">Go back to <a href="https://readmedium.com/properties-introduction-fd98bc9c1056">Properties — Introduction</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/backing-fields-e0efd8ef6895">Backing Fields</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Accessors

A quick introduction to accessors (getters and setters) in Kotlin, default accessors, custom accessors, and val vs. var in the context of properties

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #KOTLIN FEATURE

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

When not specified, default getters and setters (i.e. return field and field = value) are automatically generated by the compiler. Kotlin also adds syntactic sugar that allows working with properties as if they were fields, so obj.x is actually obj.getX() and obj.x = y is actually obj.setX(y). It is important to remember that this is only syntactic sugar, and it is the accessor functions that actually get called.

If you need to customize the accessors, use get() and/or set():

Equipped with this knowledge, we can state the technical difference between properties defined using val or var: val doesn't generate a setter, while var does.

Go back to Properties — Introduction, jump to the Table of Contents, or continue to Backing Fields.

Join me in Etnetera
Kotlin
Java
Programming
Getters And Setters
Properties
Recommended from ReadMedium