Is it possible to initialize an companion object
before the init
block in a Kotlin class? If so, how? If not, is there a way to accomplish the same thing.
I have the following scenario,
class A(val iname: String) {
init {
foo.add(this)
}
companion object B {
@JvmField val STATIC = A("hi")
@JvmField val foo = mutableListOf<A>()
}
fun printAllStatics() {
for (a in foo) {
print(a.iname)
}
}
}
and calling printAllStatics
causes a null-pointer exception.