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 →
What to know first
- factoryA factory is a function, method, or component that creates and returns values or objects while encapsulating construction or selection logic.
- static methodA static method is a function associated with a type rather than an instance, callable without an instance receiver.
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 rather than an instance, callable without an instance receiver.
- constructor methoduses · A constructor method is a type-associated operation involved in creating an instance and establishing its initial state.
- static fieldalternative to · A static field is a named variable associated with a class or program scope, rather than stored separately for each object instance.
- factoryimplements · A factory is a function, method, or component that creates and returns values or objects while encapsulating construction or selection logic.
Questions about companion object
What is 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.
Which Manning books explain companion object?
Kotlin in Action, 2E and Functional Programming in Scala, Second Edition.
What should I know before learning companion object?
Manning books that teach companion object usually introduce factory and static method first.
What is the difference between companion object and static method?
A companion object is a singleton object associated with a class or type that provides members associated with the class rather than its instances. A static method is a function associated with a type rather than an instance, callable without an instance receiver.
What is companion object a kind of?
Companion object is a kind of object declaration. An object declaration is a Kotlin declaration that defines a singleton object and creates its sole instance as part of the declaration.
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.

