Home » Java Tutorial » This Keyword in Java with Examples

This Keyword in Java with Examples

This keyword in java is a keyword provided by JVM to avoid confusion. Sometimes, programmers may define local variables and instance variables with the same name. But while accessing these same-named variables in a program, the compiler gets confused and ultimately the local variable hides the value stored at the instance variable. This keyword always refers to the current class object.

Why do we use this keyword in java?

this keyword can be used to:

1. To call the current class object:

To call the current object’s method in another method. In that case, this keyword is very helpful. 

2. To avoid the confusion between the names of local and instance variable:

When the developer declares a local variable and instance variable with the same name, and at the time of assignment, the compiler’s values get confused. Hence this keyword can be used to access the value of instance variables.

3. To call methods and access its instance members:

To access instance variables and methods using this keyword inside a method.

4. To call one constructor within another:

If you have two or more constructors with different numbers of parameters in the same class. To call one constructor in another you can simply use this keyword.

There are two ways to use this keyword in java

1. this. 

Syntax :

this. current class data member

This syntax is used to access the instance variables in the code. Suppose, we have local variables and instance variables with the same name in a method. To access the local variable you can use this keyword. An example is this syntax given below

2. this() 

Syntax :

  1. For calling default constructor or no parameterized constructor
this();
  1. For calling the parameterized constructor
this(argument_1, argument_2, …);

The above syntax is used to call a constructor within another constructor. If we have two constructors in a program one is without parameters and the other is having 2 parameters. To call a parameterized constructor in the constructor without parameters we can use the above syntax.

How to use this keyword in java?

The compiler provides six ways to use this keyword in java :

  1. this used with a field
  2. this used to invoke a constructor
  3. To return the current class instance this keyword is used
  4. this used as a method parameter
  5. Used to invoke a current class method
  6. this used as an argument in the constructor call

1. this used with field or variable hiding

this keyword is very beneficial for variable hiding. If the instance variable and local variable both are having the same name then the local variable hides the instance variable. To avoid this we can use this keyword. An example is given below.

Example :

public class Demo{

    int p; //instance variables
    int q;
    
     Demo(int p, int q //local variables) {
         this.p = p; 
         this.q = q;
    }
     
     void show()
     {
         System.out.println("p = "+p+", q = "+q); // printing the values assigned to the local variables by instance variable
     }
    
    public static void main(String[] args) {
        Demo obj = new Demo(5, 10); // creating object of demo class
        obj.show();     }
    
}

Output:

p = 5, q = 10

2. this used to invoke a constructor

In this type, this() can be used to invoke a constructor. 

Example :

public class Demo{
int a;
int b;
 
Demo() //Default constructor
{
    this(27, 01); //this call will take the compiler to the parameterized constructor
    System.out.println("Inside default constructor");
}
 
Demo(int a, int b) //Parameterized constructor
{
    this.a= a;
    this.b= b;
    System.out.println("Inside parameterised constructor");
}

    public static void main(String[] args) {
     Demo obj = new Demo();   
    }
}

Output :

Inside parameterised constructor
Inside default constructor

3. To return the current class instance this keyword is used

Moreover, to return the same class instance, this keyword is used. But the return type of method should be a class type. Let’s understand this with an example.

Example :

public class NewMain {
int x1;
int y1;

NewMain() //Default constructor
{
    x1 = 100;
    y1 = 200;
}

NewMain get() //Method that returns current class instance
{
    return this;
}

void display() 
{
    System.out.println("x1 = " + x1+ " y1 = " + y1); //Displaying value of variables j and n
}
public static void main(String[] args) 
{
    NewMain obj = new NewMain();
    obj.get().display();
}
}

Output:

x1 = 100 y1 = 200

4. this used as a method parameter

To call the method from another method of the same class this keyword in java is used. Now let’s understand this type with an example

Example :

public class Demo {
int i;
Int j;

Demo() // Default constructor
{
    i = 100;
    j = 200;
}

void display(Demo obj) // Method that receives 'this' keyword as parameter
{
    System.out.println("i = " + i + " j = " + j); //printing values of i and j

}

void get()
{
    display(this); //returning the instance to the display method
}

public static void main(String[] args) 
{
    Demo obj = new Demo ();   
    obj.get();
}
}

Output :

i = 100 j = 200

5. this used to invoke a current class method

To invoke the method of the same class this keyword is used. The below example is for the same

Example :

public class Demo
{
void display()
{
    this.show();//calling fuction show()
    System.out.println("Inside display function");
}
void show() 
{
    System.out.println("Inside show function");
}
public static void main(String args[]) 
{
    Demo obj = new Demo();
    obj.display();
}
}

Output :

Inside show function
Inside display function

6. this used as an argument in the constructor call

If you have used one object in multiple classes, you can pass this keyword to the constructor. Understand this with an example.

Example :

public class B{
A obj;

B(A obj) // Parameterized constructor with the object of X as a parameter
{
this.obj = obj;
obj.display(); // calling display method of class X
}
}

class A{
int x = 45;

A() // Default Constructor that creates an object of Y with passing this as an argument in the constructor
{
    B obj = new B(this); //passing this to the class B
}

void display(){
    System.out.println("Value of x in Class X : " + x); // method to print value of x
}
 
public static void main(String[] args) {
A obj = new A();
}
}

Output :

Value of x in Class X: 45
Do you know?
1. String to Int Java
2. Super Keyword in Java
3. Fibonacci series in java
4. HashSet in Java with Examples
5. Math.random Java
6. Static Keyword in Java
7. Final Keyword in Java
8. Palindrome in Java
9. Wrapper class in Java
10. This Keyword in Java

Pin It on Pinterest