What is a companion object in Kotlin, and how do you call its members?
answer
- No 'static' in Kotlin -> companion object
- Called on the class name: User.create()
- One companion per class
- Accessible as MyClass.Companion (real object)
- const val for inlined constants
basics
~10 sIt is a special object declared inside a class with the 'companion object' keywords. Its properties and functions can be called using the class name directly, without creating an instance.
solid answer
~40 sA companion object is an object declaration nested in a class, written with 'companion object { ... }'. There is exactly one per class. Its members (functions, properties, constants) are accessed via the enclosing class name, e.g. MyClass.create(), without instantiating MyClass. It is Kotlin's idiomatic replacement for Java 'static' members since Kotlin has no 'static' keyword. The companion is itself a real singleton object instance, accessible as MyClass.Companion, so it can implement interfaces and extend classes. Members are NOT JVM statics by default — to expose true statics to Java callers you add @JvmStatic. Constants meant to be inlined use 'const val' inside the companion.
code
kotlin · 9 linesclass Circle private constructor(val radius: Double) {
companion object {
const val PI = 3.14159
fun ofDiameter(d: Double) = Circle(d / 2)
}
}
val c = Circle.ofDiameter(10.0)
println(Circle.PI)go deeper
Knows the syntax and that members are called on the class name without an instance.
Adds that it is a real singleton (MyClass.Companion) and replaces Java static.
Notes JVM-static distinction, const val, and access to private class members.
Frames companion vs top-level declarations as an API-surface and binary-compatibility choice.
## What it is Kotlin has **no `static` keyword**. To attach members to a class (not to its instances), you put them inside a **companion object** — a single object declaration nested in the class with the `companion object` keyword. ```kotlin class User(val name: String) { companion object { val DEFAULT_NAME = "Anonymous" fun create(name: String): User = User(name) } } val u = User.create("Ann") // called on the class name println(User.DEFAULT_NAME) // no instance needed ``` ## Key facts - **One per class.** A class may declare at most one companion object. - **Called on the class name.** `User.create(...)` looks like a Java static call, but mechanically it is a method call on a hidden singleton instance. - **It is a real object.** The companion is accessible explicitly as `User.Companion`. Because it is an object, it can **implement interfaces** and **extend a superclass** — something Java statics cannot do. - **Not JVM static by default.** From Java the member is reached as `User.Companion.create(...)`. Add `@JvmStatic` to a member to also generate a true static so Java can write `User.create(...)`. - **Constants:** use `const val NAME = "..."` for compile-time constants of primitive/String type; they are inlined at call sites. - **Access to private members.** Companion code can read the enclosing class's `private` members and vice versa, which is why it is the standard place for factory functions. ## Terms - **object declaration**: `object Foo { ... }` defines a singleton; a companion is an object tied to a class. - **singleton**: exactly one instance exists for the whole program. Use the companion for class-level helpers, named constants, and factory functions.
- Does Kotlin have a 'static' keyword?No. Class-level members go in a companion object (or a top-level declaration in a file).
- Can you have two companion objects in one class?No. At most one companion object per class, though you can have additional named (non-companion) nested objects.
Like a Java 'static' block, but it is actually a real singleton object riding inside the class.
saying these in an interview costs you the question
- Claiming Kotlin has a 'static' keyword
- Saying you must create an instance to call companion members
- Thinking a class can have multiple companion objects
- Believing companion members are automatically JVM statics for Java