0
0
Kotlinprogramming~10 mins

Named companion objects in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Named companion objects
Class Declaration
Define Named Companion Object
Access Companion Object Members
Use Named Companion Object Name
Program Output or Behavior
The flow shows defining a class with a named companion object and accessing its members using the companion's name.
Execution Sample
Kotlin
class MyClass {
    companion object Factory {
        fun create() = MyClass()
    }
}

val instance = MyClass.Factory.create()
Defines a class with a named companion object 'Factory' and calls its create() function.
Execution Table
StepActionEvaluationResult
1Class MyClass loadedCompanion object Factory createdFactory object ready with create()
2Call MyClass.Factory.create()Invoke create() functionNew MyClass instance created
3Assign instanceinstance = MyClass.Factory.create()instance holds MyClass object
4Program endsNo more actionsinstance available for use
💡 Program ends after creating instance using named companion object Factory.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
instancenullMyClass instanceMyClass instanceMyClass instance
Key Moments - 2 Insights
Why do we use a named companion object instead of the default unnamed one?
Naming the companion object (like 'Factory') allows us to refer to it explicitly, which can improve code clarity and avoid confusion, as shown in step 2 where we call MyClass.Factory.create().
Can we access companion object members without the companion's name?
Yes, if the companion object is unnamed, members can be accessed directly via the class name. But with a named companion, you must use the companion's name as in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'instance' after step 3?
Anull
BMyClass instance
CFactory object
Dcreate() function
💡 Hint
Check the variable_tracker row for 'instance' after step 3.
At which step is the companion object 'Factory' created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
See execution_table row 1 describing class loading and companion creation.
If the companion object was unnamed, how would the call in step 2 change?
AFactory.create()
BMyClass.Factory.create()
CMyClass.create()
Dcreate()
💡 Hint
Named companion requires the name; unnamed allows direct class access (see key_moments).
Concept Snapshot
Named companion objects in Kotlin:
- Syntax: companion object Name { ... }
- Access members via Class.Name.member
- Useful for clarity and avoiding conflicts
- Members behave like static members
- Allows factory methods or constants inside class
Full Transcript
This visual trace shows how Kotlin named companion objects work. First, the class is loaded and its named companion object 'Factory' is created. Then, we call the create() function via MyClass.Factory.create(), which returns a new MyClass instance. This instance is assigned to the variable 'instance'. The companion object must be accessed by its name when named, unlike unnamed companions where members can be accessed directly via the class name. This helps organize code and clarify usage. The variable tracker shows 'instance' changes from null to holding the new object after creation. The quiz questions reinforce understanding of companion object creation, member access, and naming effects.