I don\'t really understand the use of \'this\' in Java. If someone could help me clarify I would really appreciate it.
On this website it says: http://docs.oracle.com/ja
The idea is to make it very clear that you are providing values for x
and y
in your constructor.
Problem is now that due to the scoping rules that within the constructor x
refers to the passed value and not the field x
. Hence x = x
results in the parameter being assigned its own value and the shadowed field untouched. This is usually not what is wanted.
Hence, a mechanism is needed to say "I need another x than the one immediately visible here". Here this
refers to the current object - so this.x
refers to a field in the current object, and super
refers to the object this object extends so you can get to a field "up higher".