0
0
Kotlinprogramming~10 mins

Companion vs top-level functions decision in Kotlin - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Companion vs top-level functions decision
Start
Need function?
Is function related to class state?
YesUse companion object function
No
Use top-level function
Function called
Execution
End
Decide if a function belongs inside a companion object (related to class) or as a top-level function (independent). Then call and execute it.
Execution Sample
Kotlin
class MyClass {
  companion object {
    fun companionFun() = "Companion"
  }
}

fun topLevelFun() = "Top-level"
Defines a companion object function and a top-level function, showing two ways to organize functions.
Execution Table
StepActionFunction CalledOutput
1Call MyClass.companionFun()companionFun"Companion"
2Call topLevelFun()topLevelFun"Top-level"
3Decide function placementN/AcompanionFun inside class, topLevelFun outside
4End of callsN/AExecution complete
💡 All functions executed, decision on placement shown
Variable Tracker
VariableStartAfter Step 1After Step 2Final
OutputN/A"Companion""Top-level"N/A
Key Moments - 2 Insights
Why put a function inside a companion object instead of top-level?
Because companion object functions are tied to the class and can access its private members, as shown in step 1 of execution_table.
Can top-level functions access class private members?
No, top-level functions are independent and cannot access private class members, unlike companion functions (see step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when calling MyClass.companionFun()?
A"Top-level"
B"Companion"
Cnull
DError
💡 Hint
Check step 1 in execution_table where companionFun is called
At which step does the decision about function placement happen?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for decision making
If a function needs to access private class data, where should it be placed?
AOutside any class
BTop-level function
CCompanion object function
DIn a separate file
💡 Hint
Refer to key_moments about access to private members
Concept Snapshot
Companion object functions belong to the class and can access private members.
Top-level functions are independent and live outside classes.
Use companion functions for class-related behavior.
Use top-level functions for general utilities.
Call companion functions with ClassName.function(), top-level directly by name.
Full Transcript
This visual trace shows how to decide between companion object functions and top-level functions in Kotlin. First, if a function needs to access or relate to class state, put it inside the companion object. Otherwise, use a top-level function. The execution table shows calling both types and their outputs. Variables track outputs after each call. Key moments clarify why companion functions can access private class data, unlike top-level functions. The quiz tests understanding of outputs, decision steps, and placement rules. The snapshot summarizes the main points for quick recall.