In my awareness, non-static method will be assigned "this" variables for its class obj & all enclosing classes.
public class TestNested {
int a=4;
public static class a{
int a=5;
static int c=10;
class b{
int a=6;
void aaa(){
int a=7;
TestNested t=new TestNested();
System.out.println(this.a);
System.out.println(b.this.a);
System.out.println(TestNested.a.b.this.a);
System.out.println(TestNested.a.this.a);
System.out.println(a.this.a);
System.out.println(t.a);
System.out.println(TestNested.this.a);
}
}
}
void r(){
TestNested t=new TestNested();
TestNested.a a=new TestNested.a();
a.b b=a.new b();
b.aaa();
}
public static void main(String[] args) {
TestNested t=new TestNested();
t.r();
}
}
in this case the final statement of void aaa() is System.out.println(TestNested.this.a); will be sentenced to cause compilation error with the reason :'com.xxx.TestNested.this' cannot be referenced from static context, that's really confusing me because the this var that points to TestNested should be the non-static hidden var of the method itself,then why it can't use its own var?
Or if my awareness is wrong that each that var is assigned in each class enclosed from the method's class, but the void aaa() isn't a static method which means it can reference non-static var rite? or even the method isn't static, but if one of its enclosing class is static, it'll be automatically recognized as static member?
This is because your nested class
ais not an inner class ofTestNested. It is a static nested class, meaning that it is not linked to a particular instance ofTestNested.What instance of
TestNestedwould you expect your expressionTestNested.thisto refer to?You can see, by the way, that you're not referring to your variable
there:Which points out that the object
ais not linked totat all.In my above answer, I assumed it was clear to you what you were doing with
this. It appears it is not the case, according to your comments, so I'm going to try and clarify it here.First of all,
thisalways refers to an object: an instance of a class.Let's assume we're in the context of a non-static method in the class
b. Because the method is non-static, the code will be executed relatively to a particular instance ofb. I take the shortcut to refer to this particular instance as "the instance ofbyou're in".Since
bis an inner class ofa, no instance ofbcan exist outside an instance ofa. This means the instance ofbyou're in is enclosed in an instance ofa. I take the shortcut to refer to this particular instance as "the instance ofayou're in" (Technically, you're in abwhich is in ana).So, in this context of a non-static method of
b:thisrefers to the instance of thebyou're in. It is the standard use of this keyword.b.this,a.b.thisorTestNested.a.b.thisare the same asthishere, the difference is only that you qualify more precisely the classb.a.thisorTestNested.a.thisboth refer to the instance ofayou're in (TestNested.ais just a more precise qualification fora). Thisaobject exists becausebis an inner class ofa, which means that every instance ofbis linked to an instance ofa. This is the only way to refer to the instance ofa.TestNested.thiswould refer to the instance ofTestNestedyou're in. But you're not in any instance ofTestNested, so it does not mean anything, hence the compile error. You're in an instance ofb, which is within an instance ofa(becausebis an inner class ofa). The instance ofaexists by itself becauseais a static nested class ofTestNested, so it is not linked to an instance ofTestNested.