aspose file tools
The moose likes Java in General and the fly likes 'this' inside sub-class constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark " Watch " New topic
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
    
    4
You can also use it to call overloaded constructors, as discussed here.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: 'this' inside sub-class constructor
 
Similar Threads
Using the Keyword super
Basic question about constructors
constructor concepts...
constructors
urgent Please help