File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this

 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
One line in the class below has me confused.
The line that says:
Test4.this.flag;
I understand that the "this' refers to the current instance of the inner class, but why the need for "Test4"? Can someone help me out on this?
public final class Test4 implements A {
class Inner {
void test() {
if (Test4.this.flag); {
sample();
}
}
}
private boolean flag = false;
public void sample() {
System.out.println("Sample");
}
public Test4() {
(new Inner()).test();
}
public static void main(String args []) {
new Test4();
}
}

 
John Dale
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside the inner class, saying "this.flag" wouild refer to member flag of the inner class, because it is the class in which you are using "this". Saying "Test4.this.flag" causes it to refer to the flag member of the Test4 class, the outer class.
See what happens if you try to compile saying "this.flag" instead.
In this case, you could just say "flag" instead, and let the standard scoping rules resolve flag for you.
 
Gaia Nathan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you John for your reply. I tried it out using just "this.flag" and the compiler threw an error message saying variable flag not defined in class Test53.Inner, which is true.
Just a recap. "this.flag" refers to the flag var in the inner class while "Test53.this.flag" refers to the flag var in the outer class.
Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic