Java 'this' keyword
Tags: java, beginners, keywords
Published: 2021-06-06 23:53:11

Hello everyone!  This week I am going to go over the “this” keyword in Java.  Many of us have used it before but there are some interesting ways to implement it that I have not heard of.  The most common use is when a class is being instantiated and the constructor parameters are the same name as the class variables.  The ‘this’ keyword is used to differentiate between an instance variable and the parameter.

    this.variable = variable;

Next is using this() to invoke the current class constructor.  Often when we are programming a class we will have to create several constructors for various parameters that are passed at initialization.  Using the this() invocation allows us to call a specific constructor to initialize that object.  An example of this is if we want the default constructor to be available in the class but initialized under another constructor.  This ability gives us greater customization for how our objects are created.

```
Class {
	
	int x;
	
	Class () {
		this(3);
	}

	Class(int x) {
		this.x = x;
	}
}
```

Another use for the ‘this’ keyword is to return the current class instance.  As you can imagine, it is pretty straightforward on how to implement it looking at the code down below.  An application for this could be if the method sets the values for an object that is already initialized and you want to return the object with the updated variables.

```
public Class method(){
	class.name = “Fred”;
	class.age = 30;
	return this;
}
```

The next use case I found is if ‘this’ is a parameter of a method.  An example is if a Bicycle class is created and you have a method that takes the Bicycle class and displays what type it is.  A second method could then run this first method within it using the ‘this’ keyword as a parameter.

```
void displayBikeModel(Bike bike){
	System.out.println(bike.modelName);
}

void printBikeType(){
	displayBikeModel(this);
}
```

‘this’ can also be use to invoke a method of the current class within another method.

```
void display(){
	this.print();
}

void print(){
	System.out.println(“Hello World”);
}
```

Lastly, the ‘this’ keyword can be used as an argument in a constructor call.  In the example I will provide below, a constructor for one class may initialize an object of another class that is dependent on the first class.  This idea is hard to relay over text so I will provide the example given by the website geeksforgeeks.org to illustrate it.  You can find the link to their article in my sources.

```
// Java code for using this as an argument in constructor
// call
// Class with object of Class B as its data member
class A
{
	B obj;
	
	// Parameterized constructor with object of B
	// as a parameter
	A(B obj)
	{
		this.obj = obj;
		
	// calling display method of class B
		obj.display();
	}
	
}

class B
{
	int x = 5;
	
	// Default Constructor that create a object of A
	// with passing this as an argument in the
// constructor
	B()
	{
		A obj = new A(this);
	}
	
	// method to show value of x
	void display()
	{
		System.out.println("Value of x in Class B : " + x);
	}
	
	public static void main(String[] args) {
		B obj = new B();
	}
}
```

Before I began research for this blog post I only knew of the use cases for the ‘this’ keyword to be limited to initializing variables that had the same name as a parameter.  Now I understand that the keyword has much greater potential.  Hopefully you found this post useful and if so please make sure to like and comment down below.

**Sources:**

1. 'this' reference in Java. GeeksforGeeks. (2021, April 23). https://www.geeksforgeeks.org/this-reference-in-java/.

2. Using the this Keyword. Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects). (n.d.). https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html.

3. Kasyap, K. (2019, July 2). Can we return this keyword from a method in java? https://www.tutorialspoint.com/can-we-return-this-keyword-from-a-method-in-java.