in , ,

Java this Keyword

Java this Keyword
Java this Keyword

Java this Keyword

In this tutorial, we will find out about this keyword in Java, how and where to use them with the help of examples.

This Keyword

In Java, this watchword is used to refer to the current object inside a strategy or a constructor. For instance,

class Main {
    int instVar;

    Main(int instVar){
        this.instVar = instVar;
        System.out.println("this reference = " + this);
    }

    public static void main(String[] args) {
        Main obj = new Main(8);
        System.out.println("object reference = " + obj);
    }
}

Output

this reference = com.ThisAndThat.MyClass@74a14482
object reference = com.ThisAndThat.MyClass@74a14482

In the above example, we made an object named obj of the class Main. We at that point print the reference to the object obj and this catchphrase of the class.

Here, we can see that the reference of both obj and this is the equivalent. It implies this is only the reference to the current object.


Use of this Keyword

There are various situations where this keyword is commonly used.

Using this for Ambiguity Variable Names

In Java, it isn’t allowed to pronounce at least two factors having a similar name inside a degree (class scope or method scope). However, instance variables and parameters may have a similar name. For instance,

class MyClass {
    // instance variable
    int age;

    // parameter
    MyClass(int age){
        age = age;
    }
}

In the above program, the example variable and the parameters have a similar name: age. Here, the Java compiler is confused because of name equivocalness.

In such a circumstance, we use this watchword. For instance,

First, let’s see an example without using this keyword:

class Main {

    int age;
    Main(int age){
        age = age;
    }

    public static void main(String[] args) {
        Main obj = new Main(8);
        System.out.println("obj.age = " + obj.age);
    }
}

Output

mc.age = 0

In the above example, we have passed 8 as an incentive to the constructor. However, we are getting 0 as an output. This is on the grounds that the Java compiler gets confused in light of the vagueness in names between case the variable and the parameter.

Presently, we should revise the above code using this catchphrase.

class Main {

    int age;
    Main(int age){
        this.age = age;
    }

    public static void main(String[] args) {
        Main obj = new Main(8);
        System.out.println("obj.age = " + obj.age);
    }
}

Output

obj.age = 8

Presently, we are getting the normal output. It is on the grounds that when the constructor is called, this inside the constructor is replaced by the object obj that has called the constructor. Consequently the age variable is assigned out worth 8.

Likewise, if the name of the parameter and case variable is unique, the compiler consequently appends this catchphrase. For instance, the code:

class Main {
    int age;

    Main(int i) {
        age = i;
    }
}

is equivalent to:

class Main {
    int age;

    Main(int i) {
        this.age = i;
    }
}

this with Getters and Setters

Another common use of this keyword is in setters and getters methods of a class. For example:

class Main {
   String name;

   // setter method
   void setName( String name ) {
       this.name = name;
   }

   // getter method
   String getName(){
       return this.name;
   }

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

       // calling the setter and the getter method
       obj.setName("worldofitech");
       System.out.println("obj.name: "+obj.getName());
   }
}

Output

obj.name: worldofitech

Here, we have used this keyword:

to assign value inside the setter method
to access value inside the getter method


Using this in Constructor Overloading

While working with constructor overloading, we might have to invoke one constructor from another constructor. In such a case, we cannot call the constructor explicitly. Instead, we have to use this keyword.

Here, we use a different form of this keyword. That is, this(). Let’s take an example,

class Complex {

    private int a, b;

    // constructor with 2 parameters
    private Complex( int i, int j ){
        this.a = i;
        this.b = j;
    }

    // constructor with single parameter
    private Complex(int i){
        // invokes the constructor with 2 parameters
        this(i, i); 
    }

    // constructor with no parameter
    private Complex(){
        // invokes the constructor with single parameter
        this(0);
    }

    @Override
    public String toString(){
        return this.a + " + " + this.b + "i";
    }

    public static void main( String[] args ) {
  
        // creating object of Complex class
        // calls the constructor with 2 parameters
        Complex c1 = new Complex(2, 3); 
    
        // calls the constructor with a single parameter
        Complex c2 = new Complex(3);

        // calls the constructor with no parameters
        Complex c3 = new Complex();

        // print objects
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
    }
}

Output

2 + 3i
3 + 3i
0 + 0i

In the above example, we have used this keyword,

to call the constructor Complex(int i, int j) from the constructor Complex(int i)
to call the constructor Complex(int i) from the constructor Complex()
Notice the line,

System.out.println(c1);

Here, when we print the object c1, the object is changed over into a string. In this process, the toString() is called. Since we override the toString() technique inside our group, we get the yield as indicated by that strategy.

One of the enormous advantages of this() is to reduce the measure of copy code. Notwithstanding, we ought to be consistently cautious while utilizing this().

This is on the grounds that calling constructor from another constructor includes overhead and it is a moderate cycle. Another tremendous favorable position of using this() is to reduce the measure of copy code.

Note: Invoking one constructor from another constructor is called explicit constructor invocation.


Passing this as an Argument

We can use this keyword to pass the current object as an argument to a method. For example,

class ThisExample {
    // declare variables
    int x;
    int y;

    ThisExample(int x, int y) {
       // assign values of variables inside constructor
        this.x = x;
        this.y = y;

        // value of x and y before calling add()
        System.out.println("Before passing this to addTwo() method:");
        System.out.println("x = " + this.x + ", y = " + this.y);

        // call the add() method passing this as argument
        add(this);

        // value of x and y after calling add()
        System.out.println("After passing this to addTwo() method:");
        System.out.println("x = " + this.x + ", y = " + this.y);
    }

    void add(ThisExample o){
        o.x += 2;
        o.y += 2;
    }
}

class Main {
    public static void main( String[] args ) {
        ThisExample obj = new ThisExample(1, -2);
    }
}

Output

Before passing this to addTwo() method:
x = 1, y = -2
After passing this to addTwo() method:
x = 3, y = 0

In the above example, inside the constructor ThisExample(), notice the line,

add(this);

Here, we are calling the add() method by passing this as an argument. Since this keyword contains the reference to the object obj of the class, we can change the value of x and y inside the add() method.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Access Modifiers

Java Access Modifiers

Java final keyword

Java final keyword