Java this Keyword With Examples

What is ‘this’ Keyword in Java?

  • Java this keyword returns a reference to the current Object.
  • We can access object variable, call the method or constructor of the current object using this keyword.
  • Java ‘this’ keyword can be used to refer to any member of the current object from within an instance method or a constructor.
  • this keyword is mostly used to avoid the confusion between a class attribute and a parameter. For example, if a parameter of a member function has the same name as that of an object variable, then the functionality may not work properly.

Java this Keyword Example

Let’s say we have a class like below.

Output with ‘this’ as referance to current object

with-this-outputJava this Keyword Example

The code prints the name of the item as “car”. Now let’s see what happens when we remove ‘this’ keyword from the constructor.

Output without ‘this’ as referance to current object

with-this-outputOutput without ‘this’ Keyword

The above example shows the application of this variable for accessing object’s attribute.

We can’t create two local variables with the same name.

However, it is allowed to create one instance variable and one local variable or method parameter with the same name.

In this case, the local variable will hide the instance variable, which is called Variable Shadowing.

To solve this problem, we use this keyword with a field to point to the instance variable instead of the local variable.

General uses of ‘this’ keyword

  1. Java ‘this’ keyword can be used to refer current class instance variable.
  2. We can use this() to invoke current class constructor. We can also pass parameters in this() statement.
  3. Java ‘this’ keyword can be used to invoke current class method (implicitly).
  4. The ‘this’ keyword can be passed as an argument in method call.
  5. We can use ‘this’ keyword to return current class instance.
  6. We can use ‘this’ keyword to access object attributes in case of variable shadowing.

There is also a special type of constructor called as Copy Constructor. Java does not have a default copy constructor but we can create it explicitly using this keyword.

Copy-Constructor & display() method ‘this’ keyword

Output of Copy-Constructor & display() method

with-this-outputCopy Constructor Output

In the above code, we saw the application of ‘this’ keyword in a copy constructor and a method display().

Calling a Constructor using this keyword

Let’s look at an example to call a constructor using this keyword.

Output:

Using this keyword to call the object method

Let’s look at an example of calling the object method using this keyword.

Using this keyword in method argument

Java this Keyword to return Current Object

Let’s look at a simple example where we will use this keyword to return the current object from a method.

Conclusion

Java this keyword is helpful in getting the reference of the current object. It’s useful in accessing object attributes in case of variable shadowing. We can also use it to call the current class constructors.

By admin

Leave a Reply