The init block will execute immediately after the primary constructor. Initializer blocks effectively become part of the primary constructor.
The constructor is the secondary constructor. Delegation to the primary constructor happens as the first statement of a secondary constructor, so the code in all initializer blocks is executed before the secondary constructor body.
Example
class Sample(private var s : String) {
init {
s += "B"
}
constructor(t: String, u: String) : this(t) {
this.s += u
}
}
Think you initialized the Sample class with
Sample("T","U")
You will get a string response at variable s as "TBU"
.
Value "T"
is assigned to s from the primary constructor of Sample class.
Then immediately the init block starts to execute; it will add "B"
to the s variable.
Next it is the secondary constructor turn; now "U"
is added to the s variable to become "TBU"
.