14

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.

Filip Allberg
  • 3,941
  • 3
  • 20
  • 37

2 Answers2

14

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")
}
hotkey
  • 140,743
  • 39
  • 371
  • 326
4

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.

klimat
  • 24,711
  • 7
  • 63
  • 70