kotlinkotlin-companion

Why do we use "companion object" as a kind of replacement for Java static fields in Kotlin?


What is the intended meaning of "companion object"? So far I have been using it just to replace Java's static when I need it.

I am confused with:

:

companion object {
    val singleton by lazy { ... }
}

which seems like an unidiomatic way of doing it. What's the better way?


Solution