Introduction
Java has reserved keywords like this and super, which are reference variables used in different contexts. To understand the difference between this and super in Java, it is essential to know the features of these keywords.

In this article, we will study this and super keywords and the differences between the two.
This Keyword in Java
In Java, this keyword is a reference variable in a method or constructor that refers to the current object.
class Example {
int value;
Example (int value){
this.value = value;
System.out.println("this = " + this);
}
public static void main(String[] args) {
Example demo = new Example (49);
System.out.println("demo object = " + demo);
}
}
Output

In the example above, we created an object, demo, of the class Example. this keyword of the class is then printed, along with reference to the object demo.
Here, we can see that both demo object and this have the same reference. It implies that this is a reference to the current object.
How Is this Keyword Used?
this keyword can be used to:
1️⃣Refer Current Class Instance Variable
this keyword is mainly used to differentiate between the formal parameters and instance variables having the same name.
For example
class Mobile{
String brand;
String model;
Mobile(String brand,String model){
this.brand=brand;
this.model=model;
}
void show(){
System.out.println(brand + “ ” + model);
}
}
class Example1{
public static void main(String args[]){
Mobile mob1=new Mobile("Samsung", “GalaxyA23”);
Mobile mob2=new Mobile("Apple", “iPhone14”);
mob1.show();
mob2.show();
}
}
Output

2️⃣Invoke The Current Class Constructor
this keyword helps in constructor reusing and accomplishing constructor chaining.
For example
class Demo{
Demo(){
System.out.println("This is a demo constructor");
}
Demo(String temp){
this();
System.out.println(temp);
}
}
class Example2{
public static void main(String args[]){
Demo obj=new Demo(“Demo for this keyword”);
}
}
Output

3️⃣Invoke The Current Class Method
For invoking a current class method, this keyword is used. However, if the user does not add this keyword, the compiler automatically adds it.
For example:
class Calculator{
void addition(){
System.out.println("Addition of 4 and 3 is " + 7);
}
void multiplication(){
System.out.println("Multiplication of 4 and 3 is " + 12);
this.addition(); // this.addition() and addition() is same
}
}
class Example3{
public static void main(String args[]){
Calculator calc=new Calculator();
calc.multiplication();
}
}
Output

4️⃣Return Current Class Instance
this keyword can be returned as a statement from the method.
For example
class Demo{
Demo obj(){
return this;
}
void display(){
System.out.println("Coding Ninjas");
}
}
class Example4{
public static void main(String args[]){
new Demo().obj().display();
}
}
Output

5️⃣Pass As An Argument In The Method
this keyword can also be passed as an argument in the class method.
For example
class Example5{
void demo(Example5 obj){
System.out.println("Hello Coding Ninjas");
}
void show(){
demo(this);
}
public static void main(String args[]){
Example5 obj = new Example5();
obj.show();
}
}
Output

6️⃣Pass As An Argument In The Constructor Call
For using one object in many classes, this keyword can be passed in the constructor call.
For example
class Demo{
Example6 obj;
Demo(Example6 obj){
this.obj=obj;
}
void display(){
System.out.println(obj.str); //using data member of Example6 class
}
}
class Example6{
String str=”Hello! I’m a Ninja”;
Example6(){
Demo obj1=new Demo(this);
obj1.display();
}
public static void main(String args[]){
Example6 obj=new Example6();
}
}
Output

You can practice by yourself with the help of Online Java Compiler.