← Back to Kotlin Course | Chapter 6: Classes & Objects | Lesson 7 of 7

Companion Objects

A companion object is a special helper attached to a class itself, so you can call it without ever building an instance of that class.

Declaring a Companion Object

A companion object block inside a class holds members accessible through the class name itself, without needing to create an instance first.

Example: Declaring a Companion Object

markup
class Circle(val radius: Double) {
    companion object {
        const val PI_APPROX = 3.14159
    }
}

fun main() {
    println("Pi approximation: ${Circle.PI_APPROX}")
}

Factory Functions in a Companion Object

A common pattern is defining a factory function inside a companion object that builds and returns instances, often used to hide or customize the details of construction.

Example: Factory Functions in a Companion Object

markup
class User private constructor(val name: String) {
    companion object {
        fun createGuest(): User = User("Guest")
    }
}

fun main() {
    val guest = User.createGuest()
    println("User: ${guest.name}")
}

Constants with const val

Companion objects are a natural place to define compile-time constants using const val, which are resolved directly at compile time and accessed through the class name.

Example: Constants with const val

markup
class Circle(val radius: Double) {
    companion object {
        const val UNIT = "cm"
    }

    fun describe() = "Radius: $radius $UNIT"
}

fun main() {
    val circle = Circle(5.0)
    println(circle.describe())
}

Naming a Companion Object

A companion object can optionally be given a name, such as companion object Factory, which can then be used alongside the default class-name access for extra clarity.

Example: Naming a Companion Object

markup
class Widget private constructor(val id: Int) {
    companion object Factory {
        fun create(id: Int): Widget = Widget(id)
    }
}

fun main() {
    val widget = Widget.Factory.create(7)
    println("Widget id: ${widget.id}")
}
Common Mistakes
  1. Confusing a companion object's members with static members in Java; Kotlin has no static keyword, companion objects are the closest equivalent.
  2. Forgetting that a companion object is a real singleton object, and it can implement interfaces or have its own properties beyond simple factory functions.
  3. Trying to declare more than one companion object per class; only one companion object is allowed per class.
Chapter Summary
  • companion object { ... } inside a class declares members that belong to the class itself, not to individual instances.
  • Companion object members are accessed via ClassName.member, similar to static members in other languages.
  • A companion object can be named, but if unnamed it defaults to the name Companion.
  • Companion objects commonly hold factory functions, constants, or shared configuration related to the class.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.