DEV Community

Cover image for What Is a Companion Object in Kotlin, and How Is It Different from static?
Mayank Kumar
Mayank Kumar

Posted on Originally published at imayankkumar.com

What Is a Companion Object in Kotlin, and How Is It Different from static?

In short: a companion object is an object that lives inside a class. You call its members with the class name, like Otp.parse("482913"), the way you'd call static in Java. But it is a real object: it can implement an interface and you can pass it around. Kotlin creates it once, the first time the class is used.

Java has static. Kotlin doesn't.

Say your login screen uses a one-time password (OTP). Every code has 6 digits, and you want one place that checks a code before an Otp is created. In Java you'd write static final int LENGTH = 6 and static Otp parse(String text). In Kotlin, static isn't a keyword.

Kotlin's answer is the companion object. This post covers what it is, why it isn't the same as static, what the compiler turns it into, and how Java code calls it.

What is a companion object?

A companion object is an object declared inside a class with the companion keyword. Its members belong to the class, not to any one instance:

class Otp private constructor(val code: String) {

    companion object {
        const val LENGTH = 6

        fun parse(text: String): Otp? {
            if (text.length != LENGTH || !text.all { it.isDigit() }) return null
            return Otp(text)
        }
    }
}

val otp = Otp.parse("482913")   // an Otp
val bad = Otp.parse("12")       // null
println(Otp.LENGTH)             // 6
Enter fullscreen mode Exit fullscreen mode

Here, we call parse() and read LENGTH with the class name. We don't need an Otp first.

A few rules, checked on Kotlin 2.4.21:

  • A class can have only one. A second one fails with "Only one companion object is allowed per class."
  • Its default name is Companion. You can give it your own: companion object Factory.
  • You can't reach its members through an instance. otp.parse("482913") doesn't compile, while Java lets you call a static method on an instance.

Why use it to create objects?

The companion object sits inside the class, so it can call the private constructor. That gives you one controlled way to create instances:

// Almost right: the constructor is public
class Otp(val code: String) {
    companion object {
        fun parse(text: String): Otp? { /* check, then Otp(text) */ }
    }
}

val otp = Otp("12")   // compiles and skips the check

// Right: only parse() can create an Otp
class Otp private constructor(val code: String) {
    companion object {
        fun parse(text: String): Otp? { /* check, then Otp(text) */ }
    }
}

val otp = Otp("12")
// Cannot access 'constructor(code: String): Otp': it is private in 'Otp'.
Enter fullscreen mode Exit fullscreen mode

The first version looks fine, but anyone can call Otp("12") and get a broken code. With a private constructor, every Otp goes through parse(), so every Otp in your app has 6 digits.

On Android you'll see the same idea in a Fragment's newInstance() function and in constants like TAG and bundle keys.

How is it different from static?

The functions in a companion object are not static. They are instance members of one real object, Otp.Companion.

Because it is a real object, it can do two things static can't. It can implement an interface, and you can pass it as a value:

interface Parser<T> {
    fun parse(text: String): T?
}

class Otp private constructor(val code: String) {
    companion object : Parser<Otp> {
        override fun parse(text: String): Otp? { /* same as before */ }
    }
}

fun <T> readField(text: String, parser: Parser<T>): T? = parser.parse(text)

readField("482913", Otp)   // Otp here means Otp.Companion
Enter fullscreen mode Exit fullscreen mode

You already use this when you write coroutineContext[Job]. In kotlinx.coroutines, Job has a companion object named Key that implements CoroutineContext.Key<Job>, so the name Job works as the lookup key.

You can also add functions to a companion from outside, with an extension:

fun Otp.Companion.fromSms(sms: String): Otp? =
    Regex("\\d{6}").find(sms)?.value?.let { parse(it) }

Otp.fromSms("Your code is 105377. Do not share it.")
Enter fullscreen mode Exit fullscreen mode

This only works if the class declares a companion object, even an empty one.

What does the compiler turn it into?

Here is the decompiled Java for the first Otp (Kotlin 2.4.21, trimmed):

public final class Otp {
    public static final Otp.Companion Companion = new Otp.Companion(null);
    public static final int LENGTH = 6;
    private final String code;

    private Otp(String code) {
        this.code = code;
    }

    public static final class Companion {
        private Companion() {
        }

        public final Otp parse(String text) {
            // check, then: return new Otp(text, null);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, we can see three things:

  1. Companion is a nested class with a private constructor. Its only instance is kept in a static field of Otp. When you write Otp.parse("482913"), Kotlin calls Otp.Companion.parse("482913").
  2. parse() is an instance method, not a static one. That's why the companion can implement an interface.
  3. Properties are stored in Otp, not in Companion. const val LENGTH becomes a public constant, and the compiler copies 6 into every place that reads it.

Diagram: many Otp instances, one companion. Each Otp instance has its own code. All of them share one Otp.Companion, which holds LENGTH and parse(). You call it with the class name, and Kotlin creates it the first time Otp is used.

When is it created?

The companion is created in the static block of Otp. The JVM runs that block once, the first time your code really uses Otp, for example when you call Otp.parse(). This matches a Java static initializer, so creation is thread-safe.

Reading Otp.LENGTH doesn't count. A const val is copied into your code when it compiles, so reading it never touches Otp. I checked this with a println in the companion's init block: it printed on the first parse() call, not on the LENGTH read.

There is one companion for the whole class, shared by every instance. A var attempts inside it is shared by every OTP screen in the app and stays until the process dies. If you need state per screen, keep it in the screen's ViewModel instead.

How do you call it from Java?

From Kotlin, all of these look the same. From Java, they don't:

In the companion Java call What the compiler makes
fun parse() Otp.Companion.parse() An instance method on Companion
@JvmStatic fun parse() Otp.parse() Also a static method on Otp that calls Companion
val MAX_TRIES = 3 Otp.Companion.getMAX_TRIES() A private static field on Otp, a getter on Companion
const val LENGTH = 6 Otp.LENGTH A public static constant, copied into callers
@JvmField val HINT = "------" Otp.HINT A public static field, no getter

@JvmStatic keeps the method on Companion too, so the interface still works. const val only allows primitive types and String. For other types, use @JvmField.

Frequently asked questions

Should I use a companion object or a top-level function?
A top-level function lives in the file, next to the class. In a file named Otp.kt, Java calls it as OtpKt.parse(). Use it for helpers that don't need the class's private parts. Use a companion object when the function needs the private constructor, or when Otp.parse() reads better.

Does @JvmStatic make Kotlin calls faster?
No. Kotlin code still calls Otp.Companion.parse(). The static method is only for Java callers.

Can I have a companion object in an interface?
Yes. Job in kotlinx.coroutines is an interface, and its companion Key is what makes coroutineContext[Job] work.

What should you remember?

A companion object is one real object inside a class. You call it with the class name, it can use the class's private constructor, and Kotlin creates it the first time the class is used. It isn't static, but @JvmStatic, const val and @JvmField make it look static to Java.

If you want to test yourself, try the companion object interview questions. This post is the ninth topic in Stage 1 of my Android roadmap. The previous one is objects.

Do you keep constants in a companion object or at the top level of the file?


This post first appeared on imayankkumar.com, where you'll find new posts first.

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

This is the clearest version of this explanation I have read, because you prove it instead of just stating it. The test that sold me is the println in the companion's init block: it printed on the first parse() call, not on the LENGTH read, which makes the point about const val being copied at compile time something I can actually see, not just believe. The table is the other keeper โ€” MAX_TRIES = 3 becoming Otp.Companion.getMAX_TRIES() from Java, while const val LENGTH = 6 is just Otp.LENGTH, is the exact trap I have watched Java callers fall into. To answer your closing question, I keep constants in the companion when they belong to the class โ€” a TAG, bundle keys, a Fragment's newInstance() โ€” and top-level only for helpers that do not need the class's private parts. The var attempts warning is the one I would underline twice: one shared attempts counter across every OTP screen until the process dies is a bug waiting for a second screen. Which rule do you use to decide between const val and @JvmField when a Java caller needs the value โ€” do you reach for @JvmField by default, or only when the type forces you to?

Collapse
 
imayankkumar profile image
Mayank Kumar •

Thanks Abdullah. The var attempts one is the bug I wanted people to remember.

My rule is simple: const val when the type allows it, @JvmField when it doesn't. const val only works for primitives and String known at compile time. For anything else, like a list or an object, @JvmField gives Java a plain static field instead of a getter.

One thing to watch with const val in a library: the value is copied into the caller at compile time. If you change it, apps that use the library keep the old value until they recompile. @JvmField doesn't have that problem.