0
0
Kotlinprogramming~10 mins

Companion objects as static alternatives in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Companion objects as static alternatives
Define class
Add companion object
Declare static-like members inside companion
Access members via class name
Use members without creating instance
End
Shows how companion objects hold static-like members inside a class, accessible without creating instances.
Execution Sample
Kotlin
class MyClass {
    companion object {
        fun greet() = "Hello from companion"
    }
}

fun main() {
    println(MyClass.greet())
}
Defines a companion object with a function and calls it using the class name without creating an instance.
Execution Table
StepActionEvaluationResult
1Define class MyClass with companion objectClass and companion object createdMyClass and companion object exist
2Define function greet inside companionFunction greet() readygreet() returns "Hello from companion"
3Call MyClass.greet() in mainInvoke greet()Returns "Hello from companion"
4Print result of MyClass.greet()Output to consoleHello from companion
5Program endsNo more codeExecution stops
💡 Program ends after printing the companion object's function result
Variable Tracker
VariableStartAfter Step 2After Step 3Final
MyClassNot definedDefined with companionDefined with companionDefined with companion
companion objectNot definedDefined with greet()Defined with greet()Defined with greet()
greet()Not definedDefinedCalledDefined
Key Moments - 2 Insights
Why can we call greet() without creating an instance of MyClass?
Because greet() is inside the companion object, which acts like a static member accessible via the class name, as shown in execution_table step 3.
Is the companion object itself an instance?
Yes, the companion object is a singleton instance created with the class, but Kotlin lets you access its members like static members, as seen in step 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 4?
A"Hello from instance"
B"Hello from companion"
C"greet() called"
DNothing is printed
💡 Hint
Check the 'Result' column at step 4 in execution_table
At which step is the greet() function called?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column for when greet() is invoked in execution_table
If greet() was defined outside the companion object, how would you call it?
ACreate an instance then call instance.greet()
BYou cannot call it at all
CMyClass.greet()
DCall greet() directly without class or instance
💡 Hint
Remember companion object allows static-like calls; without it, you need an instance (see concept_flow)
Concept Snapshot
class MyClass {
    companion object {
        fun greet() = "Hello"
    }
}

Use MyClass.greet() to call without instance.
Companion objects hold static-like members inside classes.
Full Transcript
This example shows how Kotlin uses companion objects as static alternatives. First, the class MyClass is defined with a companion object. Inside the companion object, a function greet() is declared. This function can be called using the class name directly, without creating an instance of MyClass. The execution steps show defining the class and companion, calling greet(), and printing its result. Variables track the existence of the class, companion object, and greet function. Key moments clarify why greet() can be called without an instance and that the companion object is a singleton instance. The quiz tests understanding of output, function call timing, and calling functions outside companion objects.