Link to home
Start Free TrialLog in
Avatar of embert
embert

asked on

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

}

}

 
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

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

Avatar of embert
embert

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Jan Louwerens
Jan Louwerens
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of embert

ASKER

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
So is there any more info you need, or has your question been answered?
Avatar of embert

ASKER

thanks for the help, te issue is closed.