Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 420
Filip Allberg Avatar asked Oct 31 '16 11:10

Filip Allberg


2 Answers

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")
}
like image 73
hotkey Avatar answered Nov 10 '22 23:11

hotkey


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.

like image 39
klimat Avatar answered Nov 10 '22 23:11

klimat