• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

this

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}

 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The knights of nee want a shrubbery. And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic