<p>This tutorial is about how to use getters and setters in Kotlin with the help of simple example.</p>
<h3 class="p2"><span style="color: #000080;"><b>Getters and Setters</b></span></h3>
<p>Getters are used for getting value of the property and Setters are used for setting the value of the property.</p>
<p>Setters can have <strong><span style="color: #000080;">visibility modifiers </span></strong><span style="color: #000000;">but </span>Getters always have the same visibility as the property.</p>
<p class="p3">Getter and Setter are the functions that are <span style="color: #0000ff;"><b>generated by default</b></span> for each class property by Kotlin.</p>
<p><span style="color: #0000ff;"><strong>Example:</strong></span></p>
<p>The following code in Kotlin</p>
<pre>class Employee {
 var name: String = ""
 val age: Int = 28
}</pre>
<p>is equivalent to</p>
<pre><span style="color: #008000;"><strong>//Default get() and set() functions look like:</strong></span>
class Employee {
 <strong><span style="color: #0000ff;">var</span></strong> name: String = ""
 get() = field
 set(value) {
 field = value
 }
 <span style="color: #0000ff;"><strong>val</strong></span> age: Int = 28
 get() = field
}</pre>
<ul>
<li>Properties declared with <span style="color: #0000ff;"><strong>var</strong></span> have both <span style="color: #008000;"><strong>get()</strong></span> and <strong><span style="color: #008000;">set()</span></strong> functions.</li>
<li>But the properties declared with <strong><span style="color: #0000ff;">val</span></strong> have only <strong><span style="color: #008000;">get()</span></strong> function. This is because values to <span style="color: #008000;"><strong>val</strong></span> cannot be reassigned.</li>
<li>The <strong><span style="color: #000080;">field</span></strong> keyword is used to represent the variable and the <strong><span style="color: #000080;">value</span></strong> is used to represent the value to be assigned to the variable( It can be changed also).</li>
<li>These <span style="color: #008000;"><strong>get() and set() </strong></span>functions are redundant as Kotlin provides them by default.</li>
</ul>
<p class="p1">When you instantiate object of the <span style="color: #008000;"><strong>Employee</strong></span> class and initialize the <span style="color: #008000;"><strong>name</strong></span> property, it is passed to the setters parameter <strong><span style="color: #0000ff;">value</span></strong> and sets <span style="color: #0000ff;"><strong>field</strong></span> to <span style="color: #008000;"><strong>value</strong></span>.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<pre>val employee = Employee()
employee.name = "Arun" <span style="color: #008000;"><strong>// calls set() function</strong></span></pre>
<p class="p1">Now, when you access properties (name, age) of the object, you will get <span style="color: #008000;"><strong>field</strong></span> because of the code <span style="color: #0000ff;"><strong>get() = field</strong></span>.</p>
<pre>println("The age of ${employee.name} is ${employee.age}")</pre>
<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>
<pre>The age of Arun is 28</pre>
<h3 class="p1"><span style="color: #000080;"><strong>Custom Getters and Setters</strong></span></h3>
<p class="p1">We can also provide <span style="color: #008000;"><b>custom</b></span> get() and set() function:</p>
<p><strong><span style="color: #0000ff;">Example:</span></strong></p>
<pre><strong><span style="color: #008000;">//custom getters and setters</span></strong>
class Employee{
 var name: String = ""
 get() {
 println("get() of name called")
 return field.toString()
 }
 set(value) {
 println("set() of name called")
 if (value.length <; 3)
 field = "INVALID NAME"
 else
 field = value
 }
 val age: Int = 28
 get(){
 println("get() of age called")
 return field
 }
}

fun main() {
 val employee = Employee()
 <strong><span style="color: #008000;">// calls set() function of name property</span></strong>
 employee.name = "Arun" 
<strong><span style="color: #008000;"> // calls get() function of name and age property</span></strong>
 println("The age of ${employee.name} is ${employee.age}")
}</pre>
<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>
<pre>set() of name called
get() of name called
get() of age called
The age of Arun is 28</pre>
<ul>
<li>The Employee class custom <span style="color: #008000;"><strong>get()</strong></span> and <strong><span style="color: #008000;">set()</span></strong> function is called for <span style="color: #0000ff;"><strong>name</strong></span> and custom <strong><span style="color: #008000;">get()</span></strong> is called for <span style="color: #0000ff;"><strong>age</strong></span>.</li>
<li>The <strong><span style="color: #008000;">set()</span></strong> function is called internally to set property value using <strong><span style="color: #008000;">employee.name = &#8220;Arun&#8221;.</span></strong></li>
<li>The <strong><span style="color: #008000;">get()</span></strong> function is called internally to get property value using <strong><span style="color: #008000;">employee.name</span></strong>, <strong><span style="color: #008000;">employee.age.</span></strong></li>
</ul>


