How to use companion objects in Kotlin

To call a method or member of a class in any programming language, such as C++, Java, or Kotlin, we use an object of that class. We first create the class object and then call the respective method.

Calling a method using an object

A simple example of calling a method using an object in Kotlin is given below:

Error: Code Widget Crashed, Please Contact Support

In the above code, we create the class TestClass and then the method testMethod() inside it. In the main function, we create the object of TestClass and then call testMethod() using this object.

Calling a method using a class name

We can call the method of a class using a class name in some programming languages, such as in C# or Java. We declare the methods or members of the class by using the static keyword, following which we don’t need to create an object to call these methods.

A simple example of calling static and non-static methods in Java is given below:

class TestClass
{
public static void staticMethod()
{
System.out.println("I am staticMethod.");
}
public void nonStaticMethod()
{
System.out.println("I am nonStaticMethod.");
}
}
class Abc
{
public static void main(String args[])
{
// Calling static method
TestClass.staticMethod();
// Calling non-static method
TestClass obj = new TestClass();
obj.nonStaticMethod();
}
}
Calling static and non-static methods in Java

In the above code, we create static and non-static methods in the class TestClass. In the class Abc, we call the method staticMethod() using the class name and the method nonStaticMethod() using the object of the class.

We can also call the method or member of a class using a class name in Kotlin, but here we do not have the static keyword. We can use the companion object here instead.

The companion object

We call the member of a class using a class name in Kotlin by creating the companion object as a member inside the class.

A simple example is given below:

class ClassA {
companion object CompanionObject {
fun testMethod() = println("I am testMethod inside CompanionObject.")
}
}
fun main(args: Array<String>) {
ClassA.testMethod()
}
Calling a method using a class name in Kotlin

We add the companion keyword in front of the object declaration to create a companion object. Inside the companion object, we add the members of the class. These members can be called using the class name. Here we call testMethod() using the class name.

We can also remove CompanionObject (the name of the companion object) as in the example below:

class ClassA {
companion object {
var name: String = "David"
fun testMethod() = println("I am testMethod inside companion object.")
}
}
fun main(args: Array<String>) {
println(ClassA.name)
ClassA.testMethod()
}
Calling members of a class after removing the companion object name

In the code above, we remove the name of the companion object and then call members (variable name and the testMethod() method) of a class ClassA using the class name.

Unlock your potential: Kotlin series, all in one place!

To continue your exploration of Kotlin, check out our series of Answers below:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved