I've noticed that companion objects behave in a la...
# announcements
m
I've noticed that companion objects behave in a lazy-fashion of sorts. For example, having
Copy code
open class B() {
    init {
        println("Instance of B created!")
    }
}

class A {
    companion object Companion: B()
}
B's
init
won't be called unless I access
A
in any way, or create an instance of it (
A.Companion
,
A()
). So this isn't just like Java's
static
(as the docs point out anyway: "Note that, even though the members of companion objects look like static members in other languages, *at runtime those are still instance members of real objects, and can, for example, implement interfaces*"). But this lazy-thing is something I wasn't expecting. Can I expect this behaviour no matter the platform?
e
a. expected due to how JVM works, class initializers only run as needed b. no, https://youtrack.jetbrains.com/issue/KT-40768
m
epic, thanks