Java this Keyword

What is this Keyword in Java?

  • Whenever a method needs to refer to the object that invoked it, this keyword is used.
  •  this keyword can be used inside any method or constructor to refer to the current object.
  •  this keyword is always a reference to the object on which the method was invoked.

Need Of this Keyword:

  • It is illegal in java to declare two local variables with the same name inside the same or enclosing scopes which means when a local variable has the same name as an instance variable, the local variable hides the instance variable.
  • To avoid confusion between the instance variables/class attributes and local variables/parameters with the same name, this keyword can be used.

Example 1:

Java Code

public class takeUforward {
  int x;

  // Constructor with a parameter
  public takeUforward(int x) {
	x = x; // x is printed as 0
 }


  // Call the constructor
  public static void main(String[] args) {
	takeUforward myObj = new takeUforward(7);
	System.out.println("Value of x = " + myObj.x);
  }
}

Output: Value of x = 0

Example 2 : Program with this  keyword

Java Code

public class takeUforward {
  int x;

 
 // Constructor with a parameter
  public takeUforward(int x) {
	this.x = x; // this represents the current object
 }

  // Call the constructor
  public static void main(String[] args) {
	takeUforward myObj = new takeUforward(7);
	System.out.println("Value of x = " + myObj.x);
  }
}

Output: Value of x = 7

Explanation:

  • this keyword is differentiating between the local and the instance variable “x”  having the same name as “x”.
  • If this keyword is omitted then the compiler is confused between the local and the instance variable.

Note:

  • The use of this keyword can sometimes be confusing, and some programmers are careful to avoid the same names for instance variables and formal parameters that hide instance variables.
  • Other programmers believe that it is a good convention to use the same names for clarity, and use this keyword to overcome the instance variable hiding.
  • It’s generally the programmer’s style or way of working to adopt such a practice of using this keyword.

Use Cases of this Keyword:

Example 3: Setters & Getters.

Java Code

public class takeUforward {
  String name;

  // setter method
  void setName(String name) {
    this.name = name;
  }

  // getter method
  String getName() {
    return this.name;
  }

  public static void main(String[] args) {

    takeUforward obj = new takeUforward();

    obj.setName("takeUforward"); // calling the setter method
    System.out.println("obj.name: " + obj.getName()); // calling the getter method
  }
}

Output:

obj.name: takeUforward

Example 4:  Pass this as an argument in the method call.

Java Code

public class takeUforward {

  // declare variables
  int x;
  int y;

  takeUforward(int x, int y) {
    // assign values of variables inside constructor takeUforward 
    this.x = x;
    this.y = y;

    // value of x and y before calling sum()
    System.out.println("Before passing this to sum() method:");
    System.out.println("x = " + this.x + ", y = " + this.y + ", Sum = " + 
    (this.x + this.y));

    // call the sum() method passing this as argument
    sum(this);

    // value of x and y after calling sum()
    System.out.println("After passing this to sum() method:");
    System.out.println("x = " + this.x + ", y = " + this.y + ", Sum = " + 
    (this.x + this.y));
  }

  void sum(Main o) {
    o.x += 2;
    o.y += 2;
  }
  public static void main(String[] args) {

    takeUforward obj = new takeUforward(10, -8);
  }
}

Output:

Before passing this to sum() method:
x = 10, y = -8, Sum = 2

After passing this to sum() method:
x = 12, y = -6, Sum = 6

Explanation:

Here we are employing this keyword in two ways:

  1. First, we are differentiating between the instance variable and parameterized variables “x” and “y”.
  1. We are then passing this keyword as an argument to the sum method and showing the difference in outputs.

Special thanks to Abbas Hussain Muzammil for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article