Updated April 15, 2023
Introduction to Kotlin Getter
The kotlin getter is one feature for retrieving the values using the get() method. The getters are used to get the variables and their values. The getters are the auto-generated code, and they are automatically created when we declare the values and their properties. We can override the getter with the some extending class by defining the variable as open in the base class, and then we can use and override with the help of the override keyword and its extending class. The properties values are mutable and immutable, so the values are change and readable format in the code.
Syntax
In the kotlin application, we can store and retrieve the datas by using the setter and getter method. Therefore, it comes under the database related concept for performing and optimized the web-based application.
class classname {
var vars: datatype = ""
get() = variable // getter method for getting the values
set(vars1) { // setter method for to set the values
variable = vars1
}
}
fun main(args: Array<String>) {
val x = classname()
x.vars = "" // To access the setter value
println(c.vars) // To access the getter value
}
The above codes are the basic syntax for to utilising the getter and setter values on the application. It is useful for storing and retrieve the datas.
How does Getter Function work in Kotlin?
The getter function is one of the parallel functions in the kotlin language, like setting and getting the db or back-end port values. Generally, the setter and getter methods are auto-generated, and it defines the own property and its access with the same name as the property of the object. We can get the field value because of the method like get(), and it represents the value is to be assigned to the specific variable. It also be changed whenever we want to modify the variable value in the code, and also, the get() method is redundant; one, it provides the default values on the code.
The moment which converted other languages like java code to kotlin language. We can internally generate the default getter method for the mutable properties in the kotlin language and also the getter (only) for the read-only properties. It calls the getters internally whenever we can access or modify the property using the dot notation. The annotation specifies the code that mapped to the elements for the specified annotation that can be required with the parameter; it must be the specified instance for calling the function that will be mapped with the elements.
Examples of Kotlin Getter
Given below are the examples of Kotlin Getter:
Example #1
Code:
package one;
class demo{
var x: String = "Welcome To My Domain"
get() = field
set(eg) {
field = eg
}
}
interface first {
val test: Int
fun eg() : String
fun dem() {
println("Welcome To My Domain its the first example that related to the kotlin getter function")
}
}
class ex:first{
override val test:Int=14
override fun eg()="Please try again"
}
inline fun <reified T> sam(lt: List<Any>): Boolean {
lt.forEach {
if (it is T) {
System.out.println("Welcome To My Domain its the first example that related to the kotlin getter function")
return true
}
}
System.out.println("Thank you users kindly write the code that relates to the kotlin getter function")
return false
}
fun main(args: Array<String>) {
val p = demo()
p.x = "Have a Nice Day users please try again"
println(p.x)
val vars = ex()
vars.dem()
sam<String>(listOf("raman", 3, args))
val second = mapOf<String, Int>("Employee ID" to 1256, "SNO" to 9865, "ProofID" to 45673)
for ((k,v) in second) {
println("Your input key is: $k and your input value is $v")
}
second.forEach { (k, v) ->
println("Your input key is: $k and your input value is $v")
}
println("Your input map size is: ${second.size}")
println("Your input entries are: ${second.entries}")
println("Thank you users your Entries list are: " + second.entries)
println("Your input Keys are:" + second.keys)
println("Your input Values are:" + second.values)
}
Output:
In the first example, we used inline and classes for to set and get the values like Employee details etc.
Example #2
Code:
class Second( uname: String, pass: String, id: Int , city: Char) {
var ci: String = uname
get() {
return field.toLowerCase()
}
var password: String = pass
set(value){
field = if(value.length > 8) value else throw IllegalArgumentException("Welcome To my Domain is a second example that relates to the kotlin getter method")
}
var id: Int = id
set(value) {
field = if(value > 1 ) value else throw IllegalArgumentException("id is greater than ")
}
var city : Char = city
set (value){
field = if(value == 'T') value else throw IllegalArgumentException("city should be tiruppur")
}
}
fun main(args: Array<String>) {
val varss = Second("Sivaraman","thanku",3,'T')
println("${varss.ci}")
varss.ci = "Welcome User"
println("${varss.ci}")
println("${varss.password}")
println("${varss.id}")
println("${varss.city}")
varss.password = "thanku"
varss.id= 7
varss.city = 'C'
}
Output:
In the second example, we used getter and setter in the class, and it also checks and throws the some error and exceptions like illegal state exception if the condition is not satisfied.
Example #3
Code:
package one
class first{
var uname:String = "Welcome To My Domain its the first input"
}
class second{
var uname:String = "Welcome To My Domain its the second input"
get() = field
set(value) {
field=value
}
}
class third{
var uname:String = "Welcome To My Domain its the third input"
private set
fun setuName(uname:String){
this.uname=uname
}
}
class four{
var uname:String = "Welcome To My Domain its the four input"
get() = field.toUpperCase()
set(value) {
field="Your user name is $value"
}
}
fun main(){
val first=first()
first.uname="Your first input is getting"
println("first : ${first.uname}")
val second=second()
second.uname="Your second input is getting"
println("second : ${second.uname}")
val third=third()
third.setuName("Your third input is getting ")
println("third : ${third.uname}")
val four=four()
second.uname="Your fourth input is getting"
println("four : ${second.uname}")
}
Output:
In the third example, we declared the four different classes, and we call the classes in the main method with their instances. In the third class, we can setuName() and get() for the third and second class, and we can call those methods also in main.
Conclusion
In kotlin, the getter is one of the default methods for retrieving the data, which is already set on the back-end code, like using another method called set(). Based on the user and project requirements, we can create custom setter and getter methods on the kotlin programming language.
Recommended Articles
This is a guide to Kotlin Getter. Here we discuss the introduction, syntax, and working of the getter function in kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –