aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cannot this an outer class instance from an inner class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cannot this an outer class instance from an inner class?" Watch "Cannot this an outer class instance from an inner class?" New topic
Author

Cannot this an outer class instance from an inner class?

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
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

Joined: Aug 25, 2003
Posts: 123
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

Joined: Mar 05, 2003
Posts: 1391
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot this an outer class instance from an inner class?