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.
The property initializers and init
blocks are executed in exactly the same order in which they are placed in a class/object body. Here's an example:
companion object B {
init {
print("1 ")
}
@JvmField val foo = mutableListOf<A>().apply { print("2 ") }
@JvmField val bar = mutableListOf<A>().apply { print("3 ") }
init {
print("4")
}
}
It will print 1 2 3 4
.
So, in your case, swapping the two declarations in the companion object
is enough:
companion object B {
@JvmField val foo = mutableListOf<A>()
@JvmField val STATIC = A("hi")
}
Just change the order of lines:
@JvmField val foo = mutableListOf<A>()
@JvmField val STATIC = A("hi")
Before you used foo
in A
but it wasn't initialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With