Companion objects let you create functions and properties that belong to a class itself, not to its instances. They work like static members in other languages.
Companion objects as static alternatives in Kotlin
class MyClass { companion object { // static-like members here val constant = 42 fun greet() = "Hello from companion" } }
The companion object is a singleton object inside the class.
You access its members using the class name, like MyClass.greet().
class Calculator { companion object { fun add(a: Int, b: Int) = a + b } } fun main() { println(Calculator.add(3, 4)) }
class Person { companion object { const val species = "Homo sapiens" } } fun main() { println(Person.species) }
class Logger { companion object { fun log(message: String) { println("LOG: $message") } } } fun main() { Logger.log("App started") }
This program converts Celsius to Fahrenheit using a companion object function. We call the function without creating an object.
class TemperatureConverter { companion object { fun celsiusToFahrenheit(celsius: Double): Double { return celsius * 9 / 5 + 32 } } } fun main() { val tempC = 25.0 val tempF = TemperatureConverter.celsiusToFahrenheit(tempC) println("$tempC°C is $tempF°F") }
Companion objects are initialized when the class is loaded, so their members are ready to use anytime.
You can name companion objects, but if you don't, the default name is Companion.
Companion object members can implement interfaces, which is useful for some design patterns.
Companion objects provide a way to create static-like members inside Kotlin classes.
They are useful for constants, factory methods, and utility functions related to the class.
You access companion object members using the class name without creating an instance.
Practice
companion object in a Kotlin class?Solution
Step 1: Understand companion object role
Companion objects allow defining members that belong to the class itself, not to instances.Step 2: Compare with instance members
Instance variables belong to objects, but companion object members behave like static members accessible via the class name.Final Answer:
To hold static-like members accessible without creating an instance -> Option AQuick Check:
Companion object = static-like members [OK]
- Thinking companion objects create instances
- Confusing companion objects with inheritance
- Assuming companion objects hold instance variables
Solution
Step 1: Recall Kotlin companion object syntax
The correct keyword order iscompanion objectinside the class.Step 2: Check each option
companion object { val x = 10 } uses the correct syntax. Options A, B, and D use incorrect keywords or order.Final Answer:
companion object { val x = 10 } -> Option BQuick Check:
companion object syntax = companion object { val x = 10 } [OK]
- Using 'static' keyword which Kotlin does not have
- Swapping 'object' and 'companion' keywords
- Declaring companion as a class
class MyClass {
companion object {
val number = 42
fun greet() = "Hello"
}
}
fun main() {
println(MyClass.number)
println(MyClass.greet())
}Solution
Step 1: Understand companion object access
Members inside companion object can be accessed using the class name without creating an instance.Step 2: Evaluate the print statements
PrintingMyClass.numberoutputs 42, andMyClass.greet()outputs "Hello".Final Answer:
42\nHello -> Option DQuick Check:
Companion members accessed via class name = 42 and Hello [OK]
- Trying to create an instance to access companion members
- Expecting compilation error due to missing instance
- Confusing companion members with instance members
class Example {
companion object {
fun show() = println("Show called")
}
}
fun main() {
val e = Example()
e.show()
}Solution
Step 1: Analyze how companion functions are accessed
Functions inside companion objects are accessed via the class name, not instances.Step 2: Check the call
Callinge.show()show()on instanceecauses an error becauseshow()is not an instance method.Final Answer:
Cannot call companion object function on instance -> Option CQuick Check:
Companion functions accessed via class, not instance [OK]
- Calling companion functions on instances
- Forgetting companion object keyword
- Expecting companion functions to be instance methods
Calculator with a companion object that provides a static method add to add two integers. Which code snippet correctly implements this and allows calling Calculator.add(5, 3) without creating an instance?Solution
Step 1: Identify correct static-like method declaration
In Kotlin, static methods are created inside companion objects.Step 2: Evaluate each option
class Calculator { companion object { fun add(a: Int, b: Int) = a + b } } correctly definesaddinside a companion object. class Calculator { fun add(a: Int, b: Int) = a + b } defines an instance method, so requires an object. object Calculator { fun add(a: Int, b: Int) = a + b } uses an object declaration, which works but is not a companion object inside a class. class Calculator { static fun add(a: Int, b: Int) = a + b } uses 'static' keyword which Kotlin does not support.Final Answer:
class Calculator { companion object { fun add(a: Int, b: Int) = a + b } } -> Option AQuick Check:
Companion object method = class Calculator { companion object { fun add(a: Int, b: Int) = a + b } } [OK]
- Using 'static' keyword which Kotlin lacks
- Defining method outside companion object
- Using object declaration instead of companion object
Ready to prove it?
Pass the test and this pattern moves unknown
