Categories: Java

this keyword in Java

this is a Java keyword that refers to the current object of a class.

Uses of this keyword:

The following are some examples of how to utilize this keyword:

  1. this can be used to refer to the current instance variable of a class.
  2. this can be used to call the method of the current class (implicitly)
  3. this() can be used to call the constructor of the current class.
  4. In the method call, this can be given as an argument.
  5. In the constructor call, this can be provided as a parameter.
  6. this can be used to get the current instance of the class from a method.
1. Using ‘this’ keyword to refer current class instance variables:
//Java code for using 'this' keyword to
//refer current class instance variables
class Test
{
 int a;
 int b;
 
 // Parameterized constructor
 Test(int a, int b)
 {
  this.a = a;
  this.b = b;
 }

 void display()
 {
  //Displaying value of variables a and b
  System.out.println("a = " + a + " b = " + b);
 }

 public static void main(String[] args)
 {
  Test object = new Test(10, 20);
  object.display();
 }
}
Output:
a = 10  b = 20
2. Using ‘this’ keyword to invoke current class method:
// Java code for using this to invoke current
// class method
class Test {

 void display()
 {
  // calling function show()
  this.show();
 
 System.out.println("display function");
 }
 
 void show() {
  System.out.println("show function");
 }
 

 public static void main(String args[]) {
  Test t1 = new Test();
  t1.display();
 }
}
Output:
show function
display function
3. Using this() to invoke current class constructor
// Java code for using this() to
// invoke current class constructor
class Test
{
 int a;
 int b;

 //Default constructor
 Test()
 {
  this(10, 20);
  System.out.println("Coderzpy default constructor \n");
 }
 
 //Parameterized constructor
 Test(int a, int b)
 {
  this.a = a;
  this.b = b;
  System.out.println("Coderzpy parameterized constructor");
 }

 public static void main(String[] args)
 {
  Test object = new Test();
 }
}
Output:
Coderzpy parameterized constructor
Coderzpy  default constructor
4. Using ‘this’ keyword as method argument:
// Java code for using 'this'
// keyword as method parameter
class Test
{
 int a;
 int b;
 
 // Default constructor
 Test()
 {
  a = 10;
  b = 20;
 }
 
 // Method that receives 'this' keyword as parameter
 void display(Test obj)
 {
  System.out.println("a = " +obj.a + " b = " + obj.b);
 }

 // Method that returns current class instance
 void get()
 {
  display(this);
 }

 public static void main(String[] args)
 {
  Test object = new Test();
  object.get();
 }
}
Output:
a = 10  b = 20
5. Using ‘this’ keyword as an argument in the constructor call:
// 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();
 }
}
Output:
Value of x in Class B : 5
6. Using ‘this’ keyword to return the current class instance
//Java code for using 'this' keyword
//to return the current class instance
class Test
{
 int a;
 int b;

 //Default constructor
 Test()
 {
  a = 10;
  b = 20;
 }
 
 //Method that returns current class instance
 Test get()
 {
  return this;
 }
 
 //Displaying value of variables a and b
 void display()
 {
  System.out.println("a = " + a + " b = " + b);
 }

 public static void main(String[] args)
 {
  Test object = new Test();
  object.get().display();
 }
}
Output:
a = 10  b = 20

Note: also read about the

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago