https://kotlinlang.org logo
#announcements
Title
# announcements
w

w_bianrytree

10/09/2019, 9:23 AM
Hey guys. Anyone knows what’s the execution order for init block and property?
Copy code
class A{
  val b:Int  = 0
  val a:Int
  init {
    a = 2
  }
}
Is there any order of a/b’s initialization? a first?
d

diesieben07

10/09/2019, 9:24 AM
Code runs in the order it appears in the source code. So in your example the initializer for
b
would run first, then the
init
block.
w

w_bianrytree

10/09/2019, 9:25 AM
But somehow I get some devices that a happens before b. 🤔 Complier optimization or something?
d

diesieben07

10/09/2019, 9:25 AM
Can you show your actual code? With just plain ints there is no real initialization is there...
w

w_bianrytree

10/09/2019, 9:27 AM
I add it in channel.
7 Views