What is a Companion Object?
Learn what a companion object is, how it differs from Java static members, and how it enables factory methods, with examples.
Expected Interview Answer
A companion object is a singleton tied to a specific class (in languages like Kotlin and Scala) that holds members logically scoped to the class itself rather than to any instance, replacing the role static members play in Java.
Where Java attaches static fields and methods directly onto a class, Kotlin and Scala instead compile a single companion singleton per class and route class-scoped calls to it. This keeps every non-companion member strictly instance-bound, which is more consistent with pure object orientation, while still allowing factory methods, constants and class-scoped utilities to live somewhere sensible. A companion object can implement interfaces and be passed around as a first-class value, something a Java static block cannot do. It is most commonly used for named factory functions (so a class can expose `create(...)` instead of only raw constructors) and for constants that belong conceptually to the type.
- Gives class-scoped state a first-class, referenceable object
- Enables named factory methods instead of only raw constructors
- Can implement interfaces, unlike static members in Java
- Keeps instance members and class members cleanly separated
AI Mentor Explanation
A national cricket board is a single, class-level office that exists independently of any individual player — it issues central contracts, sets eligibility rules and holds the trophy cabinet. Every player belongs to the sport, but only the board holds the shared administrative state that isn’t tied to any one athlete. A companion object plays the same role for a class: one singleton scoped to the type itself, holding factory logic and constants that don’t belong to any particular instance.
Step-by-Step Explanation
Step 1
Declare the companion object
Use `companion object { ... }` (Kotlin) inside the class body to create the class-scoped singleton.
Step 2
Add factory or shared members
Place constants and factory functions (e.g. create()) inside it instead of on instances.
Step 3
Access without an instance
Call members via the class name directly, e.g. ClassName.create(...), with no object needed.
Step 4
Optionally implement an interface
The companion can implement an interface, letting it be passed as a value where Java statics cannot.
What Interviewer Expects
- Correct comparison to Java static members
- Mention of the singleton-per-class nature
- A concrete factory-method use case
- Awareness that a companion object can implement interfaces
Common Mistakes
- Calling it “just Kotlin's version of static” without noting it is an actual object
- Forgetting it can implement interfaces and be passed as a value
- Assuming every class automatically has one (it must be explicitly declared)
- Confusing it with a regular nested object (which is not tied to the class this way)
Best Answer (HR Friendly)
“A companion object is a single shared object attached to a class that holds things which belong to the class itself rather than to any one instance, like factory methods or constants. It plays the role Java’s static members play, but because it’s an actual object, it can do more, like implement an interface.”
Code Example
class ApiClient {
// Class-scoped constants and factory logic, analogous to a companion object
static final String DEFAULT_HOST = "api.example.com";
private final String host;
private ApiClient(String host) { this.host = host; }
// Named factory method instead of only a raw constructor
static ApiClient createDefault() {
return new ApiClient(DEFAULT_HOST);
}
static ApiClient createFor(String host) {
return new ApiClient(host);
}
}
ApiClient client = ApiClient.createDefault(); // no instance needed to call the factoryFollow-up Questions
- How does a companion object differ from Java static members?
- Can a companion object implement an interface?
- How would you expose a factory method using a companion object?
- Is a companion object initialized lazily or eagerly?
MCQ Practice
1. A companion object is best understood as?
A companion object is a single, class-scoped singleton holding members not tied to any instance.
2. A key advantage of a companion object over Java static members is?
Because it is an actual object, a companion object can implement interfaces, unlike Java statics.
3. How many companion objects can a single class have?
A class may declare at most one companion object.
Flash Cards
Companion object in one line? — A singleton scoped to a class, holding class-level members instead of instance members.
What Java concept is it closest to? — Static members, but as an actual, referenceable object.
Common use case? — Named factory methods and shared constants.
Key extra capability over Java static? — It can implement interfaces and be passed as a value.