My teacher says that when I try to access an instance variable within a method I should always use the this
keyword, otherwise I would perform a double search. A local scope search and then an instance scope search.
Example:
public class Test(){
int cont=0;
public void Method(){
System.out.println(cont);//Should I use This.cont instead?
}
}
I hope he is wrong, but I can't find any argument.
No, only use this
when you have a name conflict such as when a method parameter has the same name as an instance field that it is setting.
It can be used at other times, but many of us feel that it simply adds unnecessary verbiage to the code.