Avatar ·

What is the difference between object and companion object in Kotlin? Examples

Hi everyone! I started learning Kotlin and I can't understand the difference between a regular object inside a class and a companion object. Let's say I write a simple class for working with a database: ```kotlin class DatabaseManager { object Config { val HOST = "localhost" val PORT = 5432 } companion object { fun create(): DatabaseManager = DatabaseManager() } } ``` I see that both options create something similar to static members from Java. But why are there two different mechanisms? When should I use object and when companion object? Maybe there are some nuances with inheritance, serialization, or access from Java? I would be grateful for a detailed explanation with examples. <<>>

Key difference: object is a singleton, comp