Table of contents
1.
Introduction
2.
Object and Class in Kotlin
3.
Static Keyword
4.
Companion Object in Kotlin
5.
How to Create a Companion Object in Kotlin?
5.1.
Example of Companion Object in Kotlin
6.
Frequently Asked Questions
6.1.
What is the companion object in Kotlin?
6.2.
When should you use companion object Kotlin?
6.3.
What is the difference between companion object and object declaration in Kotlin?
6.4.
What is the difference between static and companion objects in Kotlin?
7.
Conclusion
Last Updated: Aug 13, 2025
Easy

Companion Object in Kotlin

Author Geetika Dua
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A companion object in Kotlin is a special type of object associated with a class. It allows for the creation of static methods and properties specific to that class. It's useful for defining utility functions or constants related to the class without needing an instance of the class itself.

Intro image

Before we start Companion Object, let us learn about the basic idea of Kotlin.

Object and Class in Kotlin

In Kotlin, we make objects if we have to call the method or access the class members. We access the members of the class using the object only. Let us understand this through an example.

class CodingNinjas {
    fun example() = println("We are coders")
}

fun main(args: Array<String>) {     
    val obj = CodingNinjas()
    
    // Calling example() method 
    obj.example()
}


Output

Output - "We Are Coders"

Static Keyword

In Java, we have static keywords to declare members and use without making objects. We can just call them by their class name. 

public class CodingNinjas {
    public static void do() {
        // The instructions to be placed here
    }
}


If we want to call a method named do() having a class name as CodingNinjas, we will use CodingNinjas.do().

But, Do you know there is nothing like a static method in Kotlin? So, what is the alternative? It is a Companion Object! Let us learn about the Companion Object in Kotlin.

Companion Object in Kotlin

If we have to write a function without having an instance of a class, then we can use the feature of the companion object in Kotlin. We can write it like a member of a companion object inside a class.

Companion Object in Kotlin is a way to access the class members using class names only. There is no requirement for an instance of the class in this scenario.

How to Create a Companion Object in Kotlin?

We should add the companion keyword in the front of the object's declaration.

Let us understand this through an example.

class CompanionCoding {
    companion object CompanionTask {
    }
}
val a = CompanionCoding.CompanionTask


In the above code, we first made a class (CompanionCoding) and declared an object called CompanionTask. The word companion is used so that we can write a function without having an instance of a class.

Example of Companion Object in Kotlin

Let us look at some examples of Companion Objects in Kotlin.

class Codingninjas {
    companion object CN {
        fun do() = println("Coding Ninjas")
    }
}
fun main(args: Array<String>) {
    Codingninjas.do()
}


In the above code, we first made a class (Codingninjas) and declared an object called CN. The word companion is used so that we can write a function without having an instance of a class.After that, we call a method named do() to print the result.

This code will generate the output-

“Coding Ninjas”

Output

Output - "Coding Ninjas"

Frequently Asked Questions

What is the companion object in Kotlin?

Companion object in Kotlin is a singleton object associated with a class, allowing shared behavior and properties among instances.

When should you use companion object Kotlin?

We can use companion object in Kotlin to define static members and access them without an instance, or to implement factory methods.

What is the difference between companion object and object declaration in Kotlin?

Companion object is associated with a class, while object declaration creates a single instance of a class.

What is the difference between static and companion objects in Kotlin?

Static objects in Kotlin don't exist; instead, companion objects are tied to class instances, offering shared functionality like static objects in Java.

Conclusion

In this blog, we studied the Companion Object in Kotlin. The companion object in Kotlin serves as a versatile tool for managing shared behavior and properties within a class. Its ability to define static members and factory methods enhances code organization and readability, making Kotlin a powerful language for modern software development. If you want to know more about Kotlin, you can refer to these articles-

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass