Friday, August 23, 2024

10 Example of this keyword in Java

this keyword in Java is a special keyword that can be used to represent the current object or instance of any class in Java. “this” keyword can also call the constructor of the same class in Java and is used to call overloaded constructor. In this Java tutorial, we will see how to use this keyword in Java and different examples of this in Java. this sometimes also associates with a super keyword which is used to denote an instance of the superclass in Java and can be used to call an overloaded constructor in Java.




What is this keyword in Java

The this keyword in Java represent the instance of current class. It also represent the current instance. You can use the this to get attribute of the class or call any method. 

Here are a few important points related to using this keyword in Java.

1) this keyword represents the current instance of the class.

2) You can synchronize on this in a synchronized block in Java

synchronized(this){
//this synchronized block will be locked on current instance
}



3) this keyword can be used to call an overloaded constructors in java. if used then it must be the first statement in constructor this() will call the no-argument constructor and this(parameter) will call one argument constructor with the appropriate parameter. 

Here is an example of using this() for constructor chaining:

Example of this keyword to call a constructor in Java

class Loan{
    private double interest;
    private String type;
  
    public Loan(){
       this(“personal loan”);
    }
  
    public Loan(String type){
        this.type = type;
        this.interest = 0.0;
    }  
}

4) If member variable and local variable name conflict then this can be used to refer to the member variable.

Here is an example of this with member variable:

  public Loan(String type, double interest){
        this.type = type;
        this.interest = interest;
  }

Here local variable interest and member variable interest conflict which is easily resolved by referring member variable as this.interest

5) this is a final variable in Java and you can not assign value to this. this will result in a compilation error:

this = new Loan(); //cannot assign value to final variable : this

6) you can call methods of the class by using this keyword as shown in the below example.
   
   public String getName(){
        return this.toString();
    }

7) this can be used to return object. this is a valid return value. here is an example of using as return value.

public Loan getLoan(){
 return this;
}

8) this can be used to refer static members in Java as well but its discouraged and as per best practices
this should be used on the non-static reference.

9) "this" keyword can not be used in static context i.e. inside static methods or static initializer block.
if use this inside static context you will get a compilation error as shown in below example:

  public static void main(String args){
       this.toString{}; //compilation error: non static variable this can not be used in static context
  }

10) this can also be passed as method parameters since it represents the current object of the class.


Difference between this and super keyword in Java

Now that you know quite a lot of things about this keyword, its time to see the difference between this and super keyword, which is also an important Java interview question. 

While this keyword is used to represent instance of the current class, super refers to the instance of parent class. You can use super keyword to refer public, package and protected attributes from parent class. 

You can also use super to call methods from parent class and you can also call the constructor of parent class using super()

If parent class have overloaded constructor with multiple parameter you can also pass it via super() call. 

Also here is a nice table highlighting the main difference between super and this keyword in Java:

Difference between this and super keyword in Java



example of this keyword in java tutorialThat’s all on this keyword in Java. It's one of the essential core Java concept and you should know about this and super keyword and get yourself familiar with different usage of this keyword in Java. They are also extensively used during constructor overloading and you can use this() and super() to call the overloaded constructor from same class and parent class in Java. 


Java Tutorials you may Like


6 comments:

  1. @Anonymous, Thanks for your comment and liking this Java tutorial.

    ReplyDelete
  2. What are the Java best practices related to this keyword? I heard that, it's better to use this keyword to display instance operation rather than not using it? but doesn't it clutter code. Any way eager to learn some best practices in Java.

    ReplyDelete
  3. thanks for sharing such subtle details of Java. I was wondering difference between this() and super() from long time, until I realize that they call to constructor from same class and super class. but it's not uncommon to see different variant e.g. this(1), super(1) etc, what does that mean?

    ReplyDelete
  4. and used in nested classes
    this.OuterClass.member

    ReplyDelete