I have created the following class hierarchy:
open class A {
init {
f()
}
open fun f() {
println("In A f")
}
}
class B : A() {
var x: Int = 33
init {
println("x: " + x)
}
override fun f() {
x = 1
println("x in f: "+ x)
}
init {
println("x2: " + x)
}
}
fun main() {
println("Hello World!!")
val b = B()
println("in main x : " + b.x)
}
The output of this code is
Hello World!!
x in f: 1
x: 33
x2: 33
in main x : 33
But if I change the initialization of x
from
var x: Int = 33
to
var x: Int = 0
the output shows the invocation of the method in contrast to the output above:
Hello World!!
x in f: 1
x: 1
x2: 1
in main x : 1
Does anyone know why the initialization with 0
causes a different behaviour than the one with another value?