https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
r

Raed Ghazal

01/14/2022, 1:53 PM
Hi can I have the same variable name in a class and its sub-inner class, and be able to call it in that inner class?
Copy code
class A {
   var x = 5
   
   inner class B {
       val x = "upper-class".x 
   }
}
1
👌 1
b

Bailey Pollard

01/14/2022, 2:02 PM
Copy code
class test {
    val x = 3
    inner class test2 {
        val test = x
    }
}
r

Raed Ghazal

01/14/2022, 2:03 PM
well, yes i know this is possible but I was wondering if I can use the same name of the variable
r

Rob Elliot

01/14/2022, 2:56 PM
This works for me:
Copy code
class A {
  var x = 5

  inner class B {
    val x = this@A.x
  }
}
5 Views