• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot this an outer class instance from an inner class?

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that the following compiles fine.

Now, right below line 2, I instantiate an Outer and try to use it inside doStuff() like so:
Outer outer = new Outer(); // 3
System.out.println("outer instance reference is: " + outer.this); // 4.
Line 4 won't compile, javac says that it cannot resolve the symbol outer. If we modify Line 4 by removing ".this" like
System.out.println("outer instance reference is: " + outer);
then it compiles and runs OK.
So, does this mean that we can only do OuterClassName.this, but not outerClassInstance.this in the inner class? Or does outerClassInstance.this in an inner class not make sense?
Thanks a lot
Gene
 
Dhanashree Mankar
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gene Chao:
We know that the following compiles fine.

Now, right below line 2, I instantiate an Outer and try to use it inside doStuff() like so:
Outer outer = new Outer(); // 3
System.out.println("outer instance reference is: " + outer.this); // 4.
Line 4 won't compile, javac says that it cannot resolve the symbol outer. If we modify Line 4 by removing ".this" like
System.out.println("outer instance reference is: " + outer);
then it compiles and runs OK.
So, does this mean that we can only do OuterClassName.this, but not outerClassInstance.this in the inner class? Or does outerClassInstance.this in an inner class not make sense?
Thanks a lot
Gene

====================================================================
I think we can do only outerclassname.this but not outerclassinstance.this.
cz outerclassname.this evaluates to a reference that denotes the enclosing object of the current instance of a non-static inner class.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, does this mean that we can only do OuterClassName.this, but not outerClassInstance.this in the inner class?

JLS 15.8.4 defines qualified this. It has the form ClassName.this
Or does outerClassInstance.this in an inner class not make sense?
outerClassInstance.this is not valid syntax
[ September 04, 2003: Message edited by: Marlene Miller ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic