object expression - an object that will be assigned to a variable. This variable will serve as its name. This is just for one time used object. The object that you create is an anonymous class object.
syntax 1:
val name = object{
//statement
}
==================
fun main(){
val x = object{
var y = "Your Name"
fun displaySomething() = println("$y: You are my why!")
}
x.y = "Goddess"
x.displaySomething()
}
==================
Result:
Goddess: You are my why!
Object expression is allowed to inherit from a mother class. In case there are interfaces and not just a mother class, just separate them with a comma.
syntax 2:
val name = object : MotherClass {
// statement
}
====================
fun main(){
val x = object: YourInterface{
var y = "Your name"
override fun displaySomething() = println("$y: You are my why!")
}
x.y = "Goddess"
x.displaySomething()
}
interface YourInterface{
fun displaySomething()
}
====================
Result:
Goddess: You are my why!
If object expression is used as a return statement by a local function, then, the only access to this object is thru the local function.
syntax 3:
fun methodName() = object {
//statement
}
====================
fun main(){
myOutsideFunction()
val x = MyClass()
x.myLocalMethod()
}
fun myOutsideFunction(){
val w = object{
var y = "Your name"
fun displaySomething() = println("$y: You are my why!")
}
w.y = "Oops"
w.displaySomething()
}
class MyClass{
fun myLocalMethod(){
fun myX() = object{
var y = "Your name"
fun displaySomething() = println("$y: You are my why!")
}
myX().y = "Goddess"
myX().displaySomething()
}
fun myNewLocalMethod() = object{
var y = "Your name"
}
}
====================
Result:
Oops: You are my why!
Your name: You are my why!
Note:
1. myNewLocalMethod() does not work. It has no error but you cannot find the properties and functions inside the anonymous object.
2. In myLocalMethod(), I put a local function. It is like a function within a function. I was allowed to access the properties and functions. It worked but the problem is, properties and functions in a local function is considered final. So, when I changed the property y, the changes did not take effect.
3. In myOutsideFunction(), I tried to do it like in number 1 but the same thing happened. I tried to do it like in number 2, the same thing happened too. So, what I did was to try what was just the normal creation of object expression inside the function. And it works fine.
Note: You can just access the property of the class or the local variable in an object expression.
===================
fun main(){
val obj = MyClass()
obj.myMethod()
}
class MyClass{
private val prop1 = "First Property"
fun myMethod(){
val localVar = "Local"
val x = object{
fun displaySomething() = println("$prop1: Just stay anonymous. I am a $localVar.")
}
x.displaySomething()
}
}
===================
Result:
First Property: Just stay anonymous. I am a Local.
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...
