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();
}
}
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();
}
}
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
the syntax is like this:
Test4 test4 = Test4.this
So is there any more info you need, or has your question been answered?
ASKER
thanks for the help, te issue is closed.
....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();
}