Java this Keyword

Filed Under: Java

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.


public class  Item{ 
  String name; 

  // Constructor with a parameter and this keyword
  public Item(String name) { 
    this.name = name; 
  } 

  // Call the constructor
  public static void main(String[] args) { 
    Item Obj = new Item("car"); 
    System.out.println(Obj.name);
  } 
} 

Output with ‘this’ as referance to current object

Java this keyword

Java 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.


public class  Item{ 
  String name; 

  // Constructor with a parameter and without this keyword
  public Item(String name) { 
    name = name; 
  } 

  // Call the constructor
  public static void main(String[] args) { 
    Item Obj = new Item("car"); 
    System.out.println(Obj.name);
  } 
} 

Output without ‘this’ as referance to current object

Output without this keyword

Output 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


package com.journaldev.examples;

public class Blog {
	String name;
	int popularity;
        
        // parameterized constructor
	public Blog(String name, int popularity) {
		this.name = name;
		this.popularity = popularity;
	}

	// Copy-Constructor
	public Blog(Blog b) {
		this.popularity = b.popularity;
		this.name = b.name;
	}

	public void display() {
		System.out.println("name: " + this.name);
		System.out.println("popularity: " + this.popularity + " %");
	}

	public static void main(String[] args) {
		// parameterized constructor call
		Blog obj1 = new Blog("JournalDev", 100);
		obj1.display();
		// Copy-Constructor call
		Blog obj2 = new Blog(obj1);
		obj2.display();

	}

}

Output of Copy-Constructor & display() method

Copy Constructor Output

Copy 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.


package com.journaldev.examples;

public class Data {

	Data() {
		System.out.println("default constructor");
	}

	Data(int i) {
		this();
		System.out.println("int parameter constructor");

	}

	Data(String s) {
		this(10);
		System.out.println("string parameter constructor");

	}

	public static void main(String[] args) {
		Data d = new Data(20);
		System.out.println("------");
		Data d1 = new Data("Hi");
	}

}

Output:


default constructor
int parameter constructor
------
default constructor
int parameter constructor
string parameter constructor

Using this keyword to call the object method

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


package com.journaldev.examples;

public class Data {
	
	Data() {
		this.foo();
	}

	private void foo() {
		System.out.println("foo method");
	}

	public static void main(String[] args) {
		Data d = new Data();
	}
}

Using this keyword in method argument


package com.journaldev.examples;

public class Data {

	private int id;

	public Data(int id) {
		this.id = id;
		bar(this);
	}

	private void bar(Data data) {
		System.out.println(data.id);
	}

	public static void main(String[] args) {
		Data d = new Data(20);
	}
}

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.


private Data bar(Data data) {
	System.out.println(data.id);
	return this;
}

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages