```abstract class A { abstract inner class B :...
# getting-started
c
Copy code
abstract class A {
    abstract inner class B : A() {
        init {
            // How can I access the outer class?
        }
    }
}
In that example,
this
refers to the class
B
,
this@A
refers to the parent class. How can I access the outer class?
m
easiest way is to declare a
val
in the proper scope:
Copy code
abstract class A {
  val a = this;

  abstract inner class B : A() {
    init {
      // use `a` here
    }
  }
}
c
It doesn't seem to work for me, it still uses the parent's instead of the outer class.
It's weird because the bytecode must have an instruction for this (since it's possible in Java), but it seems like there is no syntax to access it from Kotlin?
v
Make it a
private val
, then it works. Because you cannot access it from subclass, but within that scope.
K 1
c
Indeed! Thanks.
r
In that example,
this
refers to the class
B
,
this@A
refers to the parent class. How can I access the outer class?
Are you sure? It seems to work fine for me.
this@A
is referring to the outer class, and
super
is referring to the parent.
v
Look at his playground link
r
Yes, I see that when I use
val a = this
because the child will also get that field. But if I forgo that and use
this@A
as I said it works fine for me. https://pl.kotl.in/-5TZyaH2n
v
Hm, yeah, indeed. I just took the OP word for truth, my bad.
👍 1