class Host {
int x=2;
class Helper {
int x = 7;
}
public static void main(String[] args){
Host ho = new Host();
Helper he = ho.new Helper();
System.out.println(ho.x);
System.out.println(he.x);
}
}
So here I'm getting the expected output
2
7
Now I wanted to ask that, say, I want to access ho
's x
from he
.
I.e. I want something here which will print me 2
through the Helper object he
:
System.out.println(???);
I know there's no use of such a thing, I just want to clarify my concept of nested classes.
I figure that this should be possible, because the Helper object he
is sort of 'binded' to the Host object ho
. Since he
is not possible without ho
. From inside the Helper class I can do System.out.println(Host.this.x);
and it works. I can't figure out how to do it from inside main.