0
0
KotlinHow-ToBeginner · 4 min read

How to Use Companion Object in Kotlin: Syntax and Examples

In Kotlin, a companion object is a special object inside a class that allows you to define members that belong to the class itself, not to instances. You use companion object to create static-like functions or properties accessible via the class name.
📐

Syntax

A companion object is declared inside a class using the keyword companion object. It can have a name or be anonymous. Members inside it can be accessed using the class name without creating an instance.

  • companion object: declares the companion object block.
  • Members: functions or properties inside companion object act like static members.
  • Access: Use ClassName.member to access them.
kotlin
class MyClass {
    companion object {
        val constant = 42
        fun greet() = "Hello from companion!"
    }
}

fun main() {
    println(MyClass.constant)
    println(MyClass.greet())
}
Output
42 Hello from companion!
💻

Example

This example shows a class Calculator with a companion object that has a function to add two numbers. You can call this function directly on the class without creating an object.

kotlin
class Calculator {
    companion object {
        fun add(a: Int, b: Int): Int {
            return a + b
        }
    }
}

fun main() {
    val result = Calculator.add(5, 7)
    println("Sum is: $result")
}
Output
Sum is: 12
⚠️

Common Pitfalls

One common mistake is trying to access companion object members through an instance instead of the class name, which is allowed but not recommended for clarity. Another is forgetting that companion objects are singleton objects, so they cannot hold instance-specific data.

Also, naming the companion object is optional but can help when you want to implement interfaces or have multiple companion objects (rare).

kotlin
class Example {
    companion object Named {
        fun info() = "Info from named companion"
    }
}

fun main() {
    // Correct access
    println(Example.info())

    // Access via companion object name
    println(Example.Named.info())
}
Output
Info from named companion Info from named companion
📊

Quick Reference

FeatureDescriptionUsage Example
DeclarationUse companion object inside a classcompanion object { ... }
AccessAccess members via class nameClassName.member
NamingOptional to name companion objectcompanion object MyName { ... }
Static-likeMembers behave like static members in Javafun greet() = "Hi"
SingletonOnly one companion object per classclass MyClass { companion object { } }

Key Takeaways

Use companion object to create static-like members inside a Kotlin class.
Access companion object members directly via the class name without creating an instance.
Companion objects are singletons and can hold functions and properties shared by all instances.
Naming a companion object is optional but useful for implementing interfaces or clarity.
Avoid storing instance-specific data inside companion objects as they belong to the class.