What is a Companion Object in Kotlin?
Learn Kotlin’s companion object — the static-member replacement, factory methods and @JvmStatic — with examples and interview Q&A.
Expected Interview Answer
A companion object in Kotlin is a single, compiler-generated singleton declared inside a class with the `companion object` keyword that holds members callable directly on the class name, replacing the role Java’s static members play.
Kotlin has no `static` keyword; instead, a class can declare at most one companion object, and its properties and functions are accessed as if they were static (`ClassName.member`) even though under the hood it’s a real singleton instance tied to the enclosing class. Because it’s an actual object, a companion object can implement interfaces, have its own name, and be extended — capabilities Java’s static members never had. It is commonly used for factory methods (replacing static constructors) and constants, and can be marked with `@JvmStatic` to expose true static bytecode members for Java interop.
- Provides class-level members without a static keyword
- Can implement interfaces, unlike Java static members
- Natural home for factory methods and constants
- Interoperates with Java via @JvmStatic when needed
AI Mentor Explanation
A national cricket board keeps exactly one official records office attached to each team — not per player, but one shared office holding the team’s all-time stats and issuing official team merchandise. You go through Team.RecordsOffice, not through any individual player, to get that shared data. A companion object works the same way: exactly one shared singleton lives inside the class, holding class-level members you access through the class name itself.
Step-by-Step Explanation
Step 1
Declare inside the class
Use `companion object { ... }` inside a Kotlin class body; at most one per class.
Step 2
Add members
Properties and functions placed inside behave like class-level (static-style) members.
Step 3
Access via the class name
Call `ClassName.member()` directly, without needing an instance of the class.
Step 4
Interop when needed
Annotate members with @JvmStatic to expose real static bytecode for Java callers.
What Interviewer Expects
- Correct definition: one singleton per class, no static keyword in Kotlin
- Mention of factory-method and constant use cases
- Awareness that it can implement interfaces unlike Java statics
- Knowledge of @JvmStatic for Java interoperability
Common Mistakes
- Calling it just 'Kotlin’s version of static' without noting it’s a real object
- Forgetting a class can have at most one companion object
- Not knowing companion objects can implement interfaces
- Assuming @JvmStatic is required for Kotlin-only code to work
Best Answer (HR Friendly)
“A companion object is Kotlin’s replacement for Java’s static members — since Kotlin has no static keyword, you declare a companion object inside a class, and its members can be called directly on the class name. Unlike static members, it’s actually a real singleton object, so it can implement interfaces and be treated like any other object when needed, which gives more flexibility.”
Code Example
// Kotlin
class User private constructor(val name: String) {
companion object {
const val DEFAULT_NAME = "Guest"
fun createGuest(): User = User(DEFAULT_NAME)
}
}
val guest = User.createGuest() // called directly on the class, like Java static
// Equivalent Java pattern the companion object replaces:
class UserJava {
static final String DEFAULT_NAME = "Guest";
private final String name;
private UserJava(String name) { this.name = name; }
static UserJava createGuest() { return new UserJava(DEFAULT_NAME); }
}
Follow-up Questions
- How does a companion object differ from a top-level object declaration in Kotlin?
- Why does Kotlin not have a static keyword at all?
- What does @JvmStatic do and when is it needed?
- Can a companion object implement an interface? Give an example use case.
MCQ Practice
1. How many companion objects can a single Kotlin class have?
A Kotlin class may declare at most one companion object.
2. A companion object is best described as?
It is an actual singleton instance, which is why it can implement interfaces, unlike a plain static member.
3. What annotation exposes a companion object’s member as true static bytecode for Java callers?
@JvmStatic generates a real static method/field in bytecode for cleaner Java interop.
Flash Cards
Companion object in one line? — A single singleton declared inside a class that holds class-level (static-style) members.
How many per class? — At most one.
How is it different from Java static? — It is a real object, so it can implement interfaces and be treated as a value.
Java interop annotation? — @JvmStatic, to generate true static bytecode members.