• 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

Applied reasoning Q57

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
////////////////////
public class OuterTest2 {
String s = "Outer";
class Inner {
String s = "Inner";
public void run() {
System.out.print(s + " ");
System.out.println(OuterTest2.this.s);///question here
}
}
public static void main(String args[]) {
new OuterTest2().new Inner().run();
}
}
////////////////////////////
Ouptput is Inner Outer. My answer is Inner Inner. I ran the code which proved I am wrong. My question is about the "this" in the marked line. What does it refer to?
As I understand, the "this" refers to an instance of the class Inner since it is in the class Inner. Also, since used as OuterTest2.this, I think the "this" means something belong to OuterTest2.
Anybody help and clarify for me? Thanks a lot.
Tony
 
Tony Xu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I searched before and after I post, using different searching method, I found one post about this question.
here is my understanding, the "this" here is a specical means of inner class to refer to its enclosing class. Am I right? anybody clarify?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
H tony..
sorry if i confuse u more....
i am confused .....
if you think that
System.out.println(OuterTest2.this.s) should print 'Inner'...then how would u output 'Outer' thru run()
and why does a compiler error generates when we write
"OuterTest2.s" or OuterTest2.Inner.s"
Richa
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Xu:
As I understand, the "this" refers to an instance of the class Inner since it is in the class Inner. Also, since used as OuterTest2.this, I think the "this" means something belong to OuterTest2.


Hi tony,
you are right, this refers to the current instance. But in case of a non-static inner class this is "that" also
By this I mean, an inner class implicitly has "this" of its enclosing outer class as well. If an inner class didn't (also) have String s (which is "Inner") in it, it would have referred to the outer String s. Since the inner class also defines String variable s, a simple reference to "s" will give you the value inside inner class, whereas when prepended with the outer class name and "this" (OuterTest2.this) it will give value of the String s as defined it in the outer class.
Consider the example below -
<code>
<pre>
public class Scratch
{
String s = "Class Level";
public void aMethod()
{
String s = "Method Level";
System.out.print(s + " ");
System.out.println(Scratch.this.s);
}
public static void main(String args[])
{
Scratch s = new Scratch();
s.aMethod();
}
}
</pre>
The output it gives -
Method Level Class Level
</code>
I hope this makes it clear.
Regards,
- Manish


[This message has been edited by Manish Hatwalne (edited September 28, 2001).]
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[in connection with my previous post]
Actually, think of it as "local" vs. "global" for the sake of analogy only. Inner class "s" is local whereas outer class is "s" is global (for the inner class/classes and other methods as well). And "OuterTest2.this." acts as scope resolution. (to borrow from c++ terminology.)
HTH,
- Manish
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tony,
You seem to be explaining it correctly to us in your post:

Originally posted by Tony Xu:
My question is about the "this" in the marked line. What does it refer to?
As I understand, the "this" refers to an instance of the class Inner since it is in the class Inner. Also, since used as OuterTest2.this, I think the "this" means something belong to OuterTest2.
Anybody help and clarify for me? Thanks a lot.
Tony


Doesn't "s" in OuterTest = "Outer"?
--liz
------------------
Elizabeth Lester
SCJP Dreamin'
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all Just a precision.
this still refers to the instance of the inner class within an instance method of the inner class. It is OuterTest2.this who referrs to the instance of the outer class. To check it:
System.out.println(this + "\n" + OuterTest2.this);
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
more clearly,
the 'this' is used to indicate current instance.
when the flow is entered the run(), the 'this' instance included two class instances: Inner and Inner's outer OuterTest2. java uses the clas name as a prefix to distinguish these two instances.
so at this code position you can use OuterTest2.this to reference the instance of OutTest2 and you can also use Inner.this/this/(non frefix) to reference the instance of Inner.
for example in the row of ///question here, you can use
System.out.print(s); or System.out.println(this.s); or System.out.println(Inner.this.s);
to print out the Inner's s, but you can only use
System.out.println(OuterTest2.this.s);
to print out OuterTest2's s.
 
Elizabeth Lester
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Galen,
Thank you!
That's a great explanation.
--liz
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep Galen.
You are right, I understand this but probably didn't make it as clear as I should have. Thanx for your wonderful explanation and correction.
- Manish
 
Tony Xu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by galen wang:
more clearly,
when the flow is entered the run(), the 'this' instance included two class instances: Inner and Inner's outer OuterTest2. java uses the clas name as a prefix to distinguish these two instances.
so at this code position you can use OuterTest2.this to reference the instance of OutTest2 and you can also use Inner.this/this/(non frefix) to reference the instance of Inner.



Good explanation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic