Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this" Watch "this" New topic


Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
One line in the class below has me confused.
The line that says:
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); {
private boolean flag = false;
public void 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:
subject: this
It's not a secret anymore!