this keyword in java

Let’s learn what is a this keyword in java?

this keyword in java

this keyword is used to call the current class or member variables and methods. It is required when we have a parameter with same name as an instance variable or field of the class. Let me explain this keyword in java with example.

class Demo
{
   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 fields “m” and “n” of type int and method setValue() to set data.

To print values of “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 “m” and “n” should be 5 and 6. But the value is zero!!!?? Why?

Let’s analyse,

In setValue() method have parameters that are named as “m” and “n”. Meanwhile class fields are also named as “m” and “n”.

During execution, compiler is confused whether “m” on the left hand side of the assignment operator is class field or the method parameters.

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)
   {
      // 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 class fields “m” and “n” are appended with ‘this’ keyword followed by dot.

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 class field whereas “m” on the right hand side is the method parameter.

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

Value m = 5

Value n = 6


Use of this keyword

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();
   }
}

Also read – ArrayList in java