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

2

15

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.

Sexivalent answered 31/10, 2016 at 11:21 Comment(2)
IMO it is like you want. What's your error?Imprudent
Sorry, my MWE did not showcase the actual problem. My mistakeSexivalent
T
15

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")
}
Tefillin answered 31/10, 2016 at 12:15 Comment(0)
I
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.

Imprudent answered 31/10, 2016 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.