• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot this an outer class instance from an inner class?

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic