I'm reading an android app and do not understand why the constructor in the ListeningQuestion.java
uses this.variable = variable
in its own constructor, but only variable = variable
(no this) in the ListeningQuestionItem.java
. They both have variable
declared as private
in the respective class. Why the differences? Any help?

- 6,531
- 8
- 38
- 62

- 43
- 6
-
1`ListeningQutionItem` is actually using bad naming conventions. Variables should start lower-case (No hard reason, it's just convention). In that case, they would use `this.` to differentiate between the class variable and the parameter. – DeeV Feb 21 '17 at 04:03
-
java variables names are case senstive. In case of Local variables with same name as of instance variable name `this` is used to identify the instance variables. refer my answer below for more , hopefully it will be helpful http://stackoverflow.com/a/42358555/504133 – nits.kk Feb 21 '17 at 04:44
2 Answers
The constructor as declared in ListeningQuestion.java you are looking -
public ListeningQuestion(String listeningFileID, String listeningSoundAddress, String listeningImageAddress,
ArrayList<ListeningQuestionItem> listeningQuestionItems) {
this.listeningFileID = listeningFileID;
this.listeningSoundAddress = listeningSoundAddress;
this.listeningImageAddress = listeningImageAddress;
this.listeningQuestionItems = listeningQuestionItems;
}
If we see the constructor, the name of the parameter
is same as the name of private variable
.
By specifying, this
, I am explicitly asking to assign the value of parameter to the local variable. This
signifies the instance to current class.
In case I do not use this
keyword, the value will not be assigned to local private variable, but re assigned to the parameter value itself, because it has narrower scope.
It is a common convention to assign the parameter values to local variable this way.
Some stuff here and here about using this
keyword
Now if you see the constructor of ListeningQuestionItem.java,
The names of parameters and local variable, differs by case. And because there is no ambiguity, you can assign the value to private variable, without using this
, as -
ListeningQuestionID = listeningQuestionID
As per https://docs.oracle.com/javase/tutorial/java/javaOO
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called.
An object has state, and in case there are same local variables of same name as of the instance fields then fields can be accessed using this
keyword.
this.listeningFileID = listeningFileID;
In this case there is one instance variable named as listeningFileID
which is accessed using this
and other is the local variable which is passed as a parameter to the constructor.
Java variable names are case senstive. In the second case
ListeningQuestionContent = listeningQuestionContent;
Observe the first character , the field name is ListeningQuestionContent and the local variable passed as parameter is named as listeningQuestionContent. Here we do not have same names so this
js not used (although this is same as this.ListeningQuestionContent = listeningQuestionContent;
)

- 5,204
- 4
- 33
- 55
-
so in the second case, listeningQuestionContent = listeningQuestionContent will not work unless it differs by the case? – g_studio Feb 22 '17 at 00:47
-
It will not be a compile error. It will be simply assignment of local variable (method parameter) to itself. – nits.kk Feb 22 '17 at 03:53
-
local variable will shadow the instance variable. to acess the instance variable in such a case when you have same named local variable then you need to use `this.` – nits.kk Feb 22 '17 at 04:11
-
@g_studio if you found my answer helpful then you have option of accepting it – nits.kk Feb 22 '17 at 07:03