this Keyword

this keyword is the reference variable that refers to the current object

this keyword in java

Why We use this keyword

See following example

public class ThisDemo
{
    int i;
    void setvalue(int i)
    {
        i = i;
    }
    void show()
    {
        System.out.println(i);
    }
    
    public static void main(String[] arg)
    {
        ThisDemo td = new ThisDemo();
        td.setvalue(100);
        td.show();
    }    
}

In this example  instance variable has the same  name as a local variable So, the compiler gets confused and shows error

For this type of problem, we use this keyword Which refers to an instance variable

To print instance variable

We have to add this line this.i = 1;

Uses of this keyword:

Here is given the 6 usages of this keyword.

  1. this can be used to refer current class instance variable.
  2. this can be used to invoke current class method (implicitly)
  3. this() can be used to invoke the current class constructor.
  4. this can be passed as an argument in the method call.
  5. this can be passed as an argument in the constructor call.
  6. this can be used to return the current class instance from the method.

this keyword is used to refer instance variable of current class

For example :

public class ThisDemo
{
    int i;
    void setvalue(int i)
    {
        this.i = i;//here we use
    }
    void show()
    {
        System.out.println(i);
    }
    
    public static void main(String[] arg)
    {
        ThisDemo td = new ThisDemo();
        td.setvalue(100);
        td.show();
    }
    
}

this can be used to invoke current class method (implicitly)

for example

public class ThisDemo
{
    int i;
    void display()
    {
        System.out.println("hello");
        
    }
    void show()
    {
        this.display();// here we use 
    }
    
    public static void main(String[] arg)
    {
        ThisDemo td = new ThisDemo();
        td.show();
    }
    
}

this() can be used to invoke current class constructor.

For example :

public class ThisDemo
{
    int i;
     ThisDemo()
    {
        System.out.println("non perametrised constructor");
        
    }
     ThisDemo(int a)
    {
        this();//here we use
       System.out.println("perametrised constructor");
    }
    
    public static void main(String[] arg)
    {
        ThisDemo td = new ThisDemo(10);
        
    }
    
}

this can be passed as an argument in the method call.

For example :

public class ThisDemo
{
    int i;
    void m1(ThisDemo td)
    {
        System.out.println("i am in m1 method");
        
    }
    void m2()
    {
       m1(this);//here we use
    }
    
    public static void main(String[] arg)
    {
        ThisDemo td = new ThisDemo();
        td.m2();
    }
    
}

this can be passed as argument in the constructor call.

For example:

class Test
{
    Test(ThisDemo td)
    {
        System.out.println("test class constructor");
    }
}


public class ThisDemo
{
    int i;
    void m1()
    {
       Test t = new Test(this); // here we use
        
    }
    
    public static void main(String[] arg)
    {
        ThisDemo td  = new ThisDemo();
        td.m1();
    }
    
}

this can be passed as an argument in the constructor call.

For Example :

class B{
  A4 obj;
  B(A4 obj){
    this.obj=obj;
  }
  void display(){
    System.out.println(obj.data);//using data member of A4 class
  }
}

class A4{
  int data=10;
  A4(){
   B b=new B(this);
   b.display();
  }
  public static void main(String args[]){
   A4 a=new A4();
  }
}


SHARE THIS PAGE





Important Link