DEV Community

Cover image for Understanding the Difference Between this and this() in Java
Rahul Raj
Rahul Raj

Posted on

Understanding the Difference Between this and this() in Java

Introduction :

This is my first post on this community, and here I would like to explain the difference between this and this() in Java in a very simple way with proper examples.

What is this in Java?

this is a keyword and a non-static object reference variable that refers to the current object of the class.

  • this is by default present inside instance methods, instance blocks, and constructors whenever an object is created. This means there is no need to mention this keyword explicitly in our code because Java software is responsible for this, but we can write it—no problem. Also , this is related to the non-static area (instance area).
  • If instance variable and local variable have the same name and if we want to access the instance variable, then we have to mention the this keyword explicitly. This is because we have only one way to access a local variable, that is, by directly using the local variable, but we can access the instance variable either by object or object reference or both.
//example 1 : source code (java 25V)
// we access instance variable inside accept() method directly 

int num = 10;
    public void accept(){
    IO.println("Directly Access instance num value in accept()  : "+num);
    }
    void main(){
        accept();
    }
Enter fullscreen mode Exit fullscreen mode

Output :

Directly Access instance num value in accept()  : 10
Enter fullscreen mode Exit fullscreen mode
// Example 2 : Source code (Java 25V)
// Here I have to explicitly mention the this keyword 
//because the instance variable and the local variable have the same name

int num = 10;
public void accept() {
    int num = 20;
    IO.println("This is local variable num value : " + num);
    IO.println("This is Instance variable num value : " + this.num);
}

void main() {
    accept();
}
Enter fullscreen mode Exit fullscreen mode

Output :

This is local variable num value : 20
This is Instance variable num value : 10
Enter fullscreen mode Exit fullscreen mode

In Short , this is related to the current class’s instance variables and instance methods.

What is this() in Java?

this() is used to call the current class’s default constructor.

  • this() is always used inside the constructor. It means if we write it anywhere (except the constructor), then we get an error.

  • this() must be the first statement inside the constructor otherwise we get an error.

//Example 3 : 
// call default constructor by using this()

class Main
{
   Main(){
   System.out.println("You are inside default constructor called by this().");
    }
    Main(String name) {
        this();
        System.out.println("My Name is "+name);
    }

    public static void main(String[]args){
    Main m1 = new Main("Rahul Raj");

    }
}
Enter fullscreen mode Exit fullscreen mode

Output :

You are inside the default constructor called by this().
My Name is Rahul Raj
Enter fullscreen mode Exit fullscreen mode

Conclusion:

In Short ,

  • this refers to the current class’s instance variables and instance methods.

  • this() refers to the current class’s default constructor.

I hope this content makes you understand ...

Top comments (0)