I have a class with a fields called “a”. In the class I have a method and in the list of arguments of this method I also have “a”. So, which “a” I will see inside of the method? Will it be the field or it will be the argument of the method?
public class myClass {
private String a;
// Method which sets the value of the field "a".
public void setA(String a) {
a = a;
}
}
By the way, there is a similar situation. A method has some local (for method) variables whose names coincide with the names of the fields. What will the “see” the method if I refer to such a method-local variable inside the method (the field or the local variable)?
The more local scope has the priority, so the parameter
awill hide the fielda. In effect, you set the value of parameterato itself. The proper idiom to avoid name clashes (and improve readability) is to usethisto explicitly mark the class member:The same is true for local variables vs member variables: local variables hide member variables with the same name.