this keyword in java .

In this session i have discussed this keyword in Java .


'this' is a reference variable that refers the current object .


We can use the this keyword as follows .

1-this keyword refer current class instance variable .

2-this() to invoke current class constructor

3-this keyword to return the current class instance

4-this keyword as method parameter

5-this keyword to invoke current class method

6-this keyword as an argument in the constructor call


1-this keyword refer current class instance variable

If the instance variable and method parameters names are same where use this keyword to resolve the ambugity problem .

Example : Write a java program that resolve the ambugity problem .

 class A{ 
         
      int id ;
      String name;
      public void getdata(int id , String name){

            this.id=id;
            this.name=name;
      }
      public void putdata(){

        System.out.println("Id : "+id);
        System.out.println("Name : "+name);
      }
      public static void main(String args[]){  
          
          A obj=new A();
          obj.getdata(1,"Rakesh");
          obj.putdata();
      }  
  }
    
    OUTPUT : Id : 1
             Name : Rakesh
    

In above program the method parameter and instance variable are same . If we do not use this keyword it will display the id 0 and name null .


this() to invoke current class constructor

this can be used to invoke the current class constructor . It is used to reuse the constructor . In other word . It is used in constructor chaining .

Example : Write a java program that demonstrate the constructor chaining .

 class A{ 
         
      int a;
      public A(){
        
            System.out.println("This is default constructor");
      }
      public A(int a){

         this(); 
         this.a=a;
         System.out.println("This is parameterized constructor");
      }
      public void show(){

        System.out.println("a : "+a);
      }
      public static void main(String args[]){  
       A obj=new A(10);
       obj.show();
      }  
  }
    OUTPUT : this is default constructor
             this is parameterized contructor
             a : 10
    

In the above program we called the default constructor to write the this() inside the parameterized constructor .

Example : Write a program that demonstrate the real use of this() .

 class A{ 
         
      int id;
      String name,city;
      public A(int id , String name){
        
            this.id=id;
            this.name=name;
      }
      public A(int id,String name , String city){

         this(id,name); 
         this.city=city;
      }
      public void show(){

        System.out.println(id+" "+name+" "+city);
      }
      public static void main(String args[]){  
        
            A obj1=new A(1,"Rakesh");
            A obj2=new A(2,"Amit","Gorakhpur");
            obj1.show();
            obj2.show();
      }  
  }
    OUTPUT : 1 Rakesh null
             2 Amit Gorakhpur
    

this() must be the first statement in constructor .


3-this keyword to return the current class instance

We can return this as a statement in a method but the return type of method must be class type . It should not be primitive .

Example : Write a program that demonstrate this can be used as return statement in a method .

 class A{ 
         
      int id;
      String name,city;
      A get(){
        
            return this;
      }
      public void show(){

         System.out.println("Hello sir");
      }
      
      public static void main(String args[]){  
        
            A obj=new A();
            obj.get().show();
      }  
  }
    OUTPUT : Hello sir
    

4-this keyword as method parameter

We can pass this as a method parameter . It is used in event handling .

Example : Write a program that demonstrate this can passed as method parameter .

 class A{ 
        
      //method that receive this as parameter   
      public void msg(A obj){

            System.out.println("Method is invoked");
      }
      public void show(){

            msg(this);
      }
      
      public static void main(String args[]){  
        
            A obj=new A();
            obj.show();
      }  
  }
    OUTPUT : Method is invoked
    

5-this keyword to invoke current class method

this can be used to invoke the current class method . But if you do not use compiler automatically add this with method .

Example : Write a program that demonstrate this can invoke the current class method .

 class A{ 
        
         
      public void msg(){

            System.out.println("Method is invoked");
      }
      public void show(){

            this.msg();
      }
      
      public static void main(String args[]){  
        
            A obj=new A();
            obj.show();
      }  
  }
    OUTPUT : Method is invoked
    

6-this keyword as an argument in the constructor call

We can also pass this as an argument in the constructor call .

Example : Write a program that demonstrate this as argument in constructor call .

 class A{ 
        
      B obj;
      public A(B obj){
      
        this.obj=obj;
      }   
      public void msg(){

            System.out.println("x :"+obj.x);
      }
 }
 
 class B{   
 
      int x=10;
      public B(){

            A obj=new A(this);
            obj.msg();
      }
      
      public static void main(String args[]){  
        
            B obj=new B();
            
      }  
  }
    OUTPUT : x : 10
    

Welcome to AndroidATPoint.

back image                     next image