Manuel Pérez Alcolea
06/02/2021, 8:23 PMopen 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?ephemient
06/02/2021, 8:33 PMManuel Pérez Alcolea
06/02/2021, 9:17 PM