how do I understand Test4.this.flag in the example?

Please help.

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();

}

}

 
embertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan LouwerensSoftware EngineerCommented:
All your doing is accessing the member variable "flag" of the "Test4" class from with the inner class...

....also, in your posted code, the semicolon should not be in the line:
if (Test4.this.flag); {
   sample();
}

it should just be:
if (Test4.this.flag) {
   sample();
}

0
embertAuthor Commented:
I still don't understand Test4.this, what's the meaning of "this", I thought "this" refers to currrent object,  but it looked like not in this case.

As for semicolon, it doesn't raise any compile-time or run-time error, so I just leave it this way. this example comes  from a mock exam for scjp.
0
Jan LouwerensSoftware EngineerCommented:
You are correct.  The "this" does, in fact, refer to the current object. The scope of an inner class works such that it can access members of the class it's contained within.

And the semicolon is syntactically correct, but semantically incorrect. Whith it there, the sample() function will never be called. If you remove the semicolon, the program should print out the word "Sample".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

embertAuthor Commented:
OK, I found the explanation in Exam Cram Java 2.  The Test4.this is used for the instance of Inner to refer to the associacted instance of enclosing class(Test4 in this case).

the syntax is like this:

Test4 test4 = Test4.this
0
Jan LouwerensSoftware EngineerCommented:
So is there any more info you need, or has your question been answered?
0
embertAuthor Commented:
thanks for the help, te issue is closed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.