Overview - Named companion objects
What is it?
In Kotlin, a companion object is a special object inside a class that allows you to call functions and access properties without creating an instance of the class. A named companion object is simply a companion object given a specific name instead of the default 'Companion'. This helps when you want to have multiple companion objects or want clearer code. It acts like a static member in other languages but with more flexibility.
Why it matters
Named companion objects let you organize code better and avoid confusion when a class needs more than one companion object or when you want to give a meaningful name to the companion. Without this, you would be limited to a single unnamed companion object, making code less clear and harder to maintain. This feature helps keep Kotlin code clean, readable, and scalable.
Where it fits
Before learning named companion objects, you should understand basic Kotlin classes and the concept of companion objects. After this, you can explore advanced Kotlin features like object declarations, static-like members, and factory patterns that use companion objects.