MY mENU


Thursday, 9 February 2012

This Keyword in Java

This :
             If we create any object, internally a reference variable "this" will created and it points to the object.. so we can say ''this'' is a keyword that points to the current object. 
Suppose we hav created an object as Srinivas sri=new Srinivas(); 
then internally 2 reference variables "sri" and "this" will be pointed to the Srinivas object. So by using "this" we can access members of Srinivas object.
this.getName(); is use to access the getName() method for the current ClassInstance.



Eg:
class Sample {
int i;
int j;
Sample(int i, int j) {
this.i=i;
this.j=j;
}}
class Demo {
public static void main(String args[]) {
Sample s=new Sample(10,20);
}}



No comments:

Post a Comment