Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
class Box(val size: Int) {
init { println("init $size") }
val double = size * 2
init { println("double $double") }
}
fun main() {
Box(3)
}init 3 double 6
- Expecting
double 6 init 3: thinking properties are set before anyinitruns. Initializers andinitblocks run top to bottom, in the order they are written. - Expecting
double 0: thinking everyinitruns before any property.double = size * 2sits between the two blocks, so the secondinitreads6.
- What changes if
val doubleis moved below the secondinitblock? - How many
initblocks may one class have?