wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this" Watch "this" New topic
Author

this

Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
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

Joined: Feb 22, 2001
Posts: 399
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

Joined: Aug 01, 2001
Posts: 62
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
 
subject: this