| Author |
'this' inside sub-class constructor
|
Sudhanshu Mishra
Ranch Hand
Joined: May 28, 2011
Posts: 201
|
|
hi all,
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....
|
 |
Hunter McMillen
Ranch Hand
Joined: Mar 13, 2009
Posts: 490
|
|
'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
Hunter
|
"If the facts don't fit the theory, get new facts" --Albert Einstein
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
You can also use it to call overloaded constructors, as discussed here.
|
 |
 |
|
|
subject: 'this' inside sub-class constructor
|
|
|