Maybe this question was asked here before, but I couldn't find it here.
It is basic question for Java developers, but here it goes: lets say I have class A with attribute a. What is the difference between these 2 constructors:
public abstract class A
{
protected String a;
public A()
{
a = "text";
}
}
The second one:
public abstract class A
{
protected String a;
public A()
{
this.a = "text"; //Here is the same with this
}
}
In the case you've given, there's no difference. Typically this
is used to disambiguate between instance variables and local variables or parameters. For example:
public A(String a) {
this.a = a; // Assign value from parameter to local variable
}
or
public void setFoo(int foo) {
this.foo = foo;
}
Some people prefer to always use this.variableName
to make it clearer to anyone reading the code; personally I find that if the class is well designed and the methods are short enough, it's usually clear enough already and the extra qualification just adds cruft. YMMV.
See more on this question at Stackoverflow