← 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.
Syntax
kotlin
class ClassName {
    companion object {
        const val CONSTANT = value
        fun factory(): ClassName = ClassName()
    }
}

ClassName.factory()

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
// Define a class `Circle`
class Circle(val radius: Double) {
    companion object {
        const val PI_APPROX = 3.14159
    }
}

// Entry point: execution of the program starts here
fun main() {
    // Print "Pi approximation: ${Circle.PI_APPROX}" to the console, with a trailing newline
    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
// Define a class `User`
class User private constructor(val name: String) {
    companion object {
        // Define the function `createGuest` with no parameters and returning a `User`
        fun createGuest(): User = User("Guest")
    }
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `guest`, set to `User.createGuest()`
    val guest = User.createGuest()
    // Print "User: ${guest.name}" to the console, with a trailing newline
    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
// Define a class `Circle`
class Circle(val radius: Double) {
    companion object {
        const val UNIT = "cm"
    }

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

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `circle`, set to `Circle(5.0)`
    val circle = Circle(5.0)
    // Print `circle.describe()` to the console, with a trailing newline
    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
// Define a class `Widget`
class Widget private constructor(val id: Int) {
    companion object Factory {
        // Define the function `create` taking `id` and returning a `Widget`
        fun create(id: Int): Widget = Widget(id)
    }
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `widget`, set to `Widget.Factory.create(7)`
    val widget = Widget.Factory.create(7)
    // Print "Widget id: ${widget.id}" to the console, with a trailing newline
    println("Widget id: ${widget.id}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
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.