0
0
Kotlinprogramming~10 mins

Object expressions (anonymous objects) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Object expressions (anonymous objects)
Start
Create anonymous object
Initialize properties
Call methods
Use object
End
This flow shows how Kotlin creates an anonymous object, initializes it, and uses its properties and methods immediately.
Execution Sample
Kotlin
val obj = object {
    val x = 10
    fun greet() = "Hi"
}
println(obj.x)
println(obj.greet())
Creates an anonymous object with a property and a method, then prints them.
Execution Table
StepActionEvaluationResult
1Create anonymous objectobject { val x=10; fun greet() = "Hi" }Object created with x=10 and greet()
2Initialize property xx = 10x is set to 10
3Define method greet()fun greet() = "Hi"Method greet() ready
4Assign object to variable objval obj = object {...}obj references the anonymous object
5Call obj.xobj.x10
6Call obj.greet()obj.greet()"Hi"
💡 All properties and methods accessed; program ends.
Variable Tracker
VariableStartAfter 1After 2After 3Final
objnullobject createdx=10 setgreet() definedobj references object with x=10 and greet()
Key Moments - 2 Insights
Why can we access obj.x and obj.greet() even though the object has no class name?
Because the object expression creates an anonymous object with properties and methods accessible through the variable obj, as shown in steps 4-6 of the execution_table.
Is the anonymous object reusable or only created once?
The anonymous object is created once when assigned to obj (step 4). It is not reusable like a class but can be used via obj as long as it exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of obj.x at step 5?
Anull
B10
C"Hi"
DUndefined
💡 Hint
Check step 5 in execution_table where obj.x is called and returns 10.
At which step is the greet() method defined in the anonymous object?
AStep 2
BStep 4
CStep 3
DStep 6
💡 Hint
Look at step 3 in execution_table where greet() is defined.
If we add a new property y = 20 inside the object, which step would change?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Adding a property affects initialization steps, see step 2 where x is initialized.
Concept Snapshot
Object expressions create anonymous objects on the spot.
Syntax: val obj = object { val x = 10; fun greet() = "Hi" }
Use obj.property or obj.method() to access.
No class name needed.
Good for quick, one-time objects.
Full Transcript
This example shows how Kotlin creates an anonymous object using an object expression. The object has a property x set to 10 and a method greet() returning "Hi". The object is assigned to the variable obj. We then access obj.x and obj.greet() to get 10 and "Hi" respectively. The execution table traces each step: creating the object, initializing properties, defining methods, assigning to obj, and calling members. The variable tracker shows obj starting as null and ending referencing the anonymous object. Key moments clarify why we can use obj.x and obj.greet() even without a class name, and that the object is created once. The quiz tests understanding of values at steps and effects of adding properties. The snapshot summarizes the concept simply.