this keyword in java

Let’s learn this keyword in java.

this keyword in java

this keyword is the reference variable that refers to the current object. this keyword refers to current class instance variable.

Let’s understand this keyword in java. For example,

import java.util.*;
class Demo
{
   // instance variable
   int m;
   int n;
   public void setValue(int m, int n)
   {
      m = m;
      n = n;
   }
   public void showValue()
   {
      System.out.println("Value m = " + m);
      System.out.println("Value n = " + n);
   }
}
public class ThisKeywordDemo
{
   public static void main(String[] args)
   {
      Demo obj = new Demo();
      obj.setValue(5, 6);
      obj.showValue();
   }
}


In the above example I have created class “Demo” with instance variables “m” and “n” of type int and method setValue() to set data.

Also read – continue statement in java

To print values of instance variables “m” and “n” we have created method showValue().

When you run the above program you will get output as shown below,

Value m = 0

Value n = 0

Our expected output for instance variables “m” and “n” should be 5 and 6. But the value is zero!!!?? Why?

Let’s analyse,

In setValue() method local variables as “m” and “n”. Meanwhile instance variables are also named as “m” and “n”.

During execution, compiler is confused whether “m” on the left hand side of the assignment operator is instance variable or the local variable.

Also read – constructor in java

Hence it does not set the value of “m” and “n” when setValue() method is called. This problem is solved by using this keyword.

Let’s see java program using ‘this’ keyword.

class Demo
{
   int m;
   int n;
   public void setValue(int m, int n)
   {
      // java this keyword
      this.m = m;
      this.n = n;
   }
   public void showValue()
   {
      System.out.println("Value m = " + m);
      System.out.println("Value n = " + n);
   }
}
public class ThisExample
{
   public static void main(String[] args)
   {
      Demo obj = new Demo();
      obj.setValue(5,6);
      obj.showValue();
   }
}


In setValue() method instance variables “m” and “n” are appended with ‘this’ keyword followed by dot operator.

Also read – operators in java

During code execution when object calls method setValue(), keyword this is replaced by the object’s handler ‘obj’ like this,

obj.m = m;

obj.n = n;

Now the compiler know that “m” on the left hand side is the instance variable whereas “m” on the right hand side is the local variable.

The variables are initialized correctly and expected output is as shown below.

Value m = 5

Value n = 6


this keyword with example

Suppose, say, if user chooses different variable names for instance variable and local variables like this,

class Demo
{
   int m;
   int n;
   public void setValue(int a, int b)
   {
      // no need of "this" keyword here because instance variable and local variables are different
      m = a;
      n = b;
   }
   public void showValue()
   {
      System.out.println("Value m = " + m);
      System.out.println("Value n = " + n);
   }
}


In this case we must create two objects of class Demo, each calling method setValue() and showValue() like this,

public static void main(String[] args)
{
   Demo obj1 = new Demo();
   obj1.setValue(8,6);
   obj1.showValue();
   Demo obj2 = new Demo();
   obj2.setValue(2,6);
   obj2.showValue();
}


Now how will the compiler decide whether it is supposed to work on the instance variable of obj1 or obj2?

Also read – static keyword in java

Well compiler implicitly appends the instance variable with this keyword.

this.m = a;

this.n = b;

Here when obj1 calls method setValue(), instance variable is appended by its reference variable. Similarly for obj2.

obj1.m = a;

obj1.n = b;

obj2.m = a;

obj2.n = b;

This process is taken care by compiler and you do not need to append this keyword explicitly unless instance variable name and local variable name are same.


Uses of this keyword in java

this keyword in java

Let’s see each use with an example.

Example: this keyword is used to invoke current class instance variable

class Example
{
   // instance variable
   int a;
   void setValues(int a)
   {
      // if this keyword is not used, then output will be default  
      // value of instance variable 'a'
      this.a = a;
   }
   void printValues()
   {
      System.out.println(a);
   }
   public static void main(String[] args)
   {
      Example obj = new Example();
      obj.setValues(5);
      obj.printValues();
   }
}


Output:

5


Example: this keyword can be used to invoke current class method implicitly

class Example
{
   void print()
   {
      System.out.println("flower brackets");
   }
   void display()
   {
      // implicitly uses this keyword if not added.
      // is equal to this.print();
      print();
   }
   public static void main(String[] args)
   {
      Example obj = new Example();
      obj.display();
   }
}


Output:

flower brackets


Example: this() keyword used to invoke current class constructor

class Example
{
   Example()
   {
      System.out.println("no argument constructor.");
   }
   Example(int a)
   {
      // invoking current class constructor
      this();
      System.out.println("parameterized constructor.");
   }
   public static void main(String[] args)
   {
      Example obj = new Example(5);
   }
}


Output:

no argument constructor.
parameterized constructor.


Example: this keyword can be used to pass as an argument in method call

class Example
{
   void display(Example ex)
   {
      System.out.println("in display() method.");
   }
   void print()
   {
      // this keyword can be used to pass as an argument in method call
      display(this);
   }
   public static void main(String[] args)
   {
      Example obj = new Example();
      obj.print();
   }
}


Output:

in display() method.


Example: this keyword can be used to pass as an argument in constructor call

class Demo
{
   Demo(Example ex)
   {
      System.out.println("in Demo class constructor.");
   }
}
class Example
{
   void display()
   {
      // this keyword can be used to pass as an argument in constructor call
      Demo d = new Demo(this);
   }
   public static void main(String[] args)
   {
      Example obj = new Example();
      obj.display();
   }
}


Output:

in Demo class constructor.


Example: this keyword can be used to return current class instance from method

class Example
{
   Example display()
   {
      // this keyword can be used to return current class instance from method
      return this;
   }
   public static void main(String[] args)
   {
      Example obj = new Example();
      obj.display();
   }
}