Kotlin - Is it possible to initialize companion object before the init block in a class?

Filip Allberg picture Filip Allberg · Oct 31, 2016 · Viewed 8.5k times · Source

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.

Answer

hotkey picture hotkey · Oct 31, 2016

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")
}