Found a super good question here on SO someone ask...
# announcements
t
t
private companion members are unavailable for
Foo
instances
Copy code
class Foo {
    init {
        println(Foo.phello) // Works
        println(Foo.hello) // ERROR
    }

    private companion object {
        val phello = "world"
        private val hello = "world"
    }
}
Usefull for singleton cases
n
hm...you sure? works for me
y
Oh wow yeah apparently it does work playground
n
so one potentially-interesting follow-up question is -- if I have private static state, should I have a public companion object with private fields, or a private companion object with "public" fields? and...that doesn't really matter too much, except that public companion objects are referenceable in Java as
Foo.Companion.INSTANCE
or whatever, even if they're empty. on the other hand, if you're going to be adding public static state soon, it's easier to make the companion object public; otherwise you have to add private to all the now-public-but-should-be-private fields after making the companion object public