what about using \"this\" with methods in Java? Is it optional or there are situations when one needs to use it obligatory?
The only situation I have encountered is
Three obvious situations where you need it:
Here's an example of all three:
public class Test
{
int x;
public Test(int x)
{
this.x = x;
}
public Test()
{
this(10);
}
public void foo()
{
Helper.doSomethingWith(this);
}
public void setX(int x)
{
this.x = x;
}
}
I believe there are also some weird situations using inner classes where you need super.this.x but they should be avoided as hugely obscure, IMO :)
EDIT: I can't think of any examples why you'd want it for a straight this.foo() method call.
EDIT: saua contributed this on the matter of obscure inner class examples:
I think the obscure case is:
OuterClass.this.foo()when accessingfoo()of the outer class from the code in an Inner class that has afoo()method as well.