Property initializers and init
blocks are executed in exactly the same order in which they are placed in the body of the class / object. Here is 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") } }
He will print 1 2 3 4
.
So, in your case, replacing two declarations in a companion object
enough:
companion object B { @JvmField val foo = mutableListOf<A>() @JvmField val STATIC = A("hi") }
source share