• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Applied Reasoning Mock #59

 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question from Applied Reasoning's Mock:
<pre>
what is printed to System.out when the following program is executed:
1: public class OuterTest2{
2: String s = "outer";
3: class Inner{
4: String s = "Inner";
5: public void run(){
6: System.out.print(s + ",");
7: System.out.println(OuterTest2.this.s);
8: }
9: }
10: public static void main(String[] args){
11:new OuterTest2.new Inner().run();
12: }
13: }
</pre>
answer: inner,outer
First of all, this isn't compiling for me. I have to change line 11 to new OuterTest2().new Inner().run();
But when I make this change the answer is right. So my question is regarding the cryptic use of this in line 7, "OuterTest2.this.s".

Is this the standard way an inner class makes a reference to the current instance of its enclosing class?

I'd never seen it before. If it's just "this.s" I get Inner, Inner, which makes perfect sense. And a class can't use "this" to refer to anything outside itself...right??? So this is what I deduce, hopefully it's right.
Thanks
Eric
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric,
Weird isn't it?
Inner classes have an implicit reference to the enclosing context. And you can obtain the implicit reference by using the special form of this ie.,
EnclosingClassName.this
Point worth remembering, especially for the people from C++ background.
Good luck.

Ajith
[This message has been edited by Ajith Kallambella (edited May 11, 2000).]
[This message has been edited by Ajith Kallambella (edited May 11, 2000).]
 
Eric Barnhill
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ajith.
 
Eric Barnhill
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to make a clarification on my earlier post.
<pre>
First of all, this isn't compiling for me. I have to change line 11 to new OuterTest2().new Inner().run();
</pre>
What happened was, I copied down the code of the problems I got wrong into a file (since I couldn't cut and paste). 2 or 3 days later when I went to study java again I looked at the code and posted a couple of questions here. This particular remark is from a mistake I made in transcribing the code, and the applet's syntax is correct. I didn't want to imply some sloppiness on the part of the applet, that was not really there, in a public forum, hence this clarification. I thought the applet was quite a good resource for learning really. It was harder than, um, lassoing an angry cow or something.
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic