The commonly used syntax:
public class Foo
{
private int x;
public Foo(int x)
{
this.x=x;
}
}
Is actually "flawed." If you have an anonymous inner class inside the Foo constructor (i.e. if this were a GUI code and you were adding an ActionListener) then the private variable x of the Foo class cannot actually be accessed, because it is being overshadowed by the local variable x. And since anonymous inner classes can't access local variables, it will report an error. So you might be inclined to solve this the same way you referenced the field variable the first time with "this.x" However, that doesn't work in this case either, because the object "this" now refers to is the anonymous inner class contained in the constructor that has no private variable x. In other words, from that inner class there is no way to access the instance variable x, because the local variable x overshadows it. The only way I can think of to get around this is to use different names for the local and instance variables so no shadowing is going on. Just an insight I had that I thought I'd share ;)
Btw, if someone does find a way to access the instance variable x under these conditions, please let me know =)