07-20-2023, 09:06 AM
I have created the following class hierarchy:
```Kotlin
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
```lang-none
Hello World!!
x in f: 1
x: 33
x2: 33
in main x : 33
```
**But** if I change the initialization of `x` from
```Kotlin
var x: Int = 33
```
to
```Kotlin
var x: Int = 0
```
the output shows the invocation of the method in contrast to the output above:
```lang-none
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?
```Kotlin
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
```lang-none
Hello World!!
x in f: 1
x: 33
x2: 33
in main x : 33
```
**But** if I change the initialization of `x` from
```Kotlin
var x: Int = 33
```
to
```Kotlin
var x: Int = 0
```
the output shows the invocation of the method in contrast to the output above:
```lang-none
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?