I have a doubt regarding a basic concept.We say that objects are created after the constructor runs,i.e constructors build the object.Then how come we use 'this' inside the constructor if the constructor itself is responsible for object building?If i am not mistaken,the object building starts from the super class constructors,and that is really when an object comes in existence.
I have these two points in my head and are confusing me.
I am a newbie,so please be patient with my lack of knowledge.
Please help me out of this question....
'this' is used to represent an instance of a class and is used often in constructors to differentiate between instance variables (the ones declared under the class definition and local variables (the ones that are the parameters in the constructor), this prevents something called Shadowing; which happens when two variables have the same name and 'shadow' each others definitions.
Hope this helps
"If the facts don't fit the theory, get new facts" --Albert Einstein
Joined: Oct 13, 2005
You can also use it to call overloaded constructors, as discussed here.