properties - are the variables defined within the body of the class that act as a global variables to the whole class. It uses val and var during declaration.
syntax:
val name1: Type
val is immutable. You cannot change the value once it is assigned. You can get a value but you cannot accept a new ones. So, this uses get() method only.
var is mutable. You are allowed to change the value. You can get and set a value. So, this uses get() and set() method.
sample:
val message = "La Isla Bonita"
println(message)
Here, we set a value "La Isla Bonita" to a variable message implicitly. And we get the value and printed it implicitly too.
Why implicit?
In reality, this is what happen in the background:
var message: String = "La Isla Bonita"
get() = field
set(value){
field = value
}
Note:
1. field here is a temporary variable use for backing properties to store a value. The field is already defined in the API's. You don't need to create a field variable. This is an implicit backing scheme.
2. We use var instead of just val because we want to set a value to a variable message.
3. We need INDENTATION when we want to do the get() and set() method explicitly.
4. La Isla Bonita is required there for initializing of the variable. You can put any value in there.
Sometimes you may want the set to be private, you can specify it like this.
var message: String = "La Isla Bonita"
private set
This means that the message has a private and default implementation of set(). The get() has default implementation too but with default modifiers.
Explicit backing scheme
private var _message: String? = null
val message: String?
get() = _message ?: throw Exception("Error")
fun setMessage(yourMessage: String){
_message = yourMessage
}
So, in explicit backing scheme, you define a variable to be use for backing. But what is a backing scheme? This scheme's purpose is to hide the variable that you are using in the program which controls everything in that class and just expose a second variable whose purpose is just for get() method. This second variable is the only access going to your class. If it has only a get() method, the outside world could not manipulate it. And in case the second variable was corrupted by the outside world, it won't affect the whole class, because your first variable is the one controlling the class.
=====================
fun main(){
val booking = Airplane("Ukraine")
booking.setName("Moshizuki Touya")
booking.startingFrom = null
booking.displayInformationTravel()
}
private const val MY_PLACE = "La Isla Bonita"
class Airplane(private val travel: String){
private lateinit var name: String
var startingFrom: String? = "My Place"
get() =
if(field == null){
MY_PLACE
}else {
field
}
set(value){
field = value
}
private val travellingTo: String
get() = travel
fun setName(name: String){
this.name = name
}
private fun getName(): String{
return name
}
fun displayInformationTravel(){
println("Name: ${getName()}")
println("From: $startingFrom")
println("To: $travellingTo")
}
}
====================
Result:
Name: Moshizuki Touya
From: La Isla Bonita
To: Ukraine
Note:
1. const - If the value is won't change. You can declare it const if it is top level property, member of an object or companion object.
2. lateinit - If you don't want the property to be initialize in the constructor, you can use the keyword lateinit. The word itself means to initialize it later but make sure that before you invoke it, you did initialize it or else it would throw an exception. You can use the isInitialized to check if your lateinit variable was really initialized.
if(name.isInitialized){ //statement}
YOU ARE READING
Kotlin Programming
RandomI have been trying to learn things online. I will put my notes in here. I put things in my own words and i do not copy their programs. I try to do things the right way so that i may learn. Below is the site where i study. Go check this out if you wa...
