Companion object
A companion object is a singleton object associated with a class or type that provides members associated with the class rather than its instances.
Kotlin in Action, 2E
A companion object is the specially designated object declaration inside a Kotlin class whose members can be accessed through the containing class name.
- How it is used
- Provides class-associated factories and operations that need private-class access without requiring an instance.
- How deep it goes
- Defines class-name access, private-constructor factories, naming, interface implementation, and extensions; defers generic details used by the factory example.
- Examples
- Demonstrates MyClass.callMe(), User creation factories, Person JSON loading, JSONFactory implementation, and a Person.Companion extension.
- What you can do after
- Apply it via Kotlin class declarations to expose factories, implement interfaces, and attach class-associated operations.
Functional Programming in Scala, Second Edition
A companion object is a Scala singleton object declared with the same name as a class or other data type and granted access to its private and protected members.
- How it is used
- Organizes construction and operations for a data type beside its constructors.
- How deep it goes
- Defines naming, access privileges, variadic construction, and recursive operations without detailing compiler implementation.
- Examples
- Uses List.apply for construction and List.sum or List.product for recursive pattern-matching operations.
- What you can do after
- Build it from scratch by defining a matching object with constructors, variadic apply, and operations for a functional data type.
Read how Functional Programming in Scala, Second Edition explains companion object →
Related concepts
- object declarationkind of · An object declaration is a Kotlin declaration that defines a singleton object and creates its sole instance as part of the declaration.
- static methodalternative to · A static method is a function associated with a type or enclosing scope rather than an object instance, and callable without an instance receiver.
- constructor methoduses · A constructor is a type-associated operation or callable that creates or initializes an instance and establishes its initial state.
- static fieldalternative to · A named variable whose storage is associated with a class or has static duration, rather than being allocated separately for each object instance or function call.
- factoryimplements · A factory is a function, method, class, or component that creates and returns objects or other values while encapsulating construction details or selection.
- objectkind of · A computational object is a runtime entity or structured value representing data, state, or a resource, with properties or operations defined by its programming model.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

