Sunday 24 July 2011

u all heard about 'this' keyowrd now i will something different from 'this'

basically we use this for differentiating instance variable from , local or parameters . ya its ok you wana explain like this .

class alpha
{
       int x ;
       alpha( int x )
       {
                x = x ;
       }
}
now which x is yours and which is of class .
for differentiating b/w x & x we use , this .
this is used , for our class variable . means instance variable .

this.x = x
means you are assigning something , to your instance variable .

=====================================================================

but now listen i want to explain something different
we all see that , you cannot call a arguement constructor from default constructor . so for this we use               this ( ) ; method . use it like this .


class alpha
{
       
       alpha( )
       {
                this ( 3 ); 
                // and make always this ( ) ; as your first statement in the constructor .
       }
       alpha( int x )
       {
                 
       }
}
======================================================================

now i want to explain ..... that treat this as a address , or a reference , which have qualities . means 
this is only built for a particular class , or for it super class , means this is same a object reference which is necessarily created in a class and used in a particular class 

if you create a object of different class and want to use this as a ' this ' then you will got a compile time error .

every thing you do with 'this' is limited to its superclass and to this class .

=======================================================================

No comments: