this is one of the most important keyword in java which refers to the current class object. Like we are creating an object for our class similarly internally a object is there called as this which points to the current class object unless we create an Object.We can say that  'this ' is not an actual object but it points to the current class object. Accessibility of ' this ' refers to the current object that means if we have more than one object in our class then this will refer to that object in which context area it is been used. 

Important points regarding this keyword 

1. It is a scope of instance context so it can only be used from the non-static context. 
2. It is an alias for the current object.
3. We can be able to assign this into other reference of the current or super class type. 
4. We cannot assign any other current class or its super class object in this as because it is defined as a final variable so content cannot be modified . 
5. ' this ' is a final variable so we cannot assign any reference. 

Some of the uses of this keyword 

1. this can be use to initialize the instance we have both local and instance member with same name.
2. this can be used to access the member from the current object.
3. this can be used while using constructor chaining i.e constructor overloading to pass the value from one overloaded constructor to another but it should be the first statement.
4. Also used to distinguish between the local variable and instance variable in any instance method . 

What if we try to access this from any static context ?
It will give an compilation error that " non-static variable this cannot be referenced from a static context " .

Why such type error occur and why we cannot access this in any static context ?
It is because static context is not a part of object and unless we create and object ' this ' cannot be used and hence we cannot guarantee that used will surely create an object in their program. That is why it is a compilation error as because compiler is very much unsure about the creation of object by the user.

Give an example showing the initializing the instance variable if local variable is of same name as of instance variable ?

public class prog {

int x ;           // Instance variable.

prog(int x) {    // In constructor parameter x is local.

this.x = x ;    //this.x is used to initialize instance var 
                 with local x. 
}
public static void main(String sush[]) {

 prog p = new prog(10);  // 10 is passed to x in constructor. 

 }
}

Give an example to show how it is been used in constructor chaining ?

public class prog {

int x ;          // instance variable.
int y ;          // Instance variable.

prog(int x){
this.x = x ;   //this.x is used to initialize instance var with 
                 local x i.e 30
}

prog(int x, int y) { 
this(30);       //30 is passed to one parameter overloaded 
                  constructor.

this.x = x ;   //this.x is used to initialize instance var 
                with local x i.e 10 
this.y = y ;   //this.y is used to initialize instance var 
                 with local y i.e 20
}

public static void main(String sush[]) {

 prog p = new prog(10,20);  // 10 is passed to x in constructor. 

 }
}

What will happen if we assign any other reference variable to ' this ' ?

If we try to assign any other current class reference or any super class reference to this variable then it will give compilation error that " cannot assign a value to final variable this " . It means it is a final variable we cannot modify the content of it. For ex -

public class prog {

int x ;           // Instance variable.

prog(int x) {  
prog p1 = new prog(); // Local object creation. 
this = p1;           // Assigning p1 to this "compilation error"
this.x = x ;        //this.x is used to initialize instance
                      var with local x. 
}
public static void main(String sush[]) {

 prog p = new prog(10);  // 10 is passed to x in constructor. 

 }
}

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic