‘this’ Keyword in Java

In this post we’re going to see one of the most important and most useful keyword i.e ‘this’ keyword in  java. This is the topic of concern because as soon as one starts with OOP concept, ‘this’ keyword is encountered more often and people just ignore it considering it as a part of syntax. But as a matter of fact it has variety of applications from coding point of view which simplifies the code and makes it more understandable than before.

I will be covering  the explanation of ‘this’ keyword in java syntax but the concept is applicable for most of the programming languages. This blog is for people who face difficulty in understanding the above keyword and I am sure that by the end of this blog y’all will level up your skills in coding.

To be discussed-

What is ‘this’ keyword and why it is used ?

Other usage of ‘this’ keyword

What is ‘this’ keyword?

‘this’ is a reference variable or simply a variable that refers to a current object.It  helps the compiler to differentiate between local variable(parametric variable) and instance variable(class variable).

Why it is used?

Let’s understand this with the help of an example,

class Abc {
  protected int studentId;    // instance variable (class  variable)
  public Abc(int id) {          // local variable (parametric variable)
  studentId = id;// assign value of local variable to instance variable 
}
public void showData() {  
 System.out.println("Student Id: " +   studentId);
}
 public static void main(String args[]) {
 Abc obj = new Abc(2);
 obj.showData();
 }
}

OUTPUT-

Student Id: 2

Here, it is clearly visible that this program accepts Student id through constructor Abc() and shows it through showData(). 

This example will never face any error as there are no naming conflict between local variables and instance variable.But what if there are more attributes related to student like name, address, phone number etc. So in that case one has to use many number of different local variables and instance variable.

public Abc(int studentId) {
studentId = studentId;  // same name for local and instance variable

here arises an error, compiler fails to understand which one is instance variable and which one is a local variable and so it assigns ‘0’ to it.

OUTPUT-

Student Id: 0

Here comes the role of ‘this’ keyword. ‘this’ keyword helps the compiler to understand that value of local variable should be assigned to instance variable invoked through current object.

public Abc(int studentId) { 
this.studentId = studentId;
}

the above piece of code is now perfect and is ready to display correct value. Here this.studentId refers to the instance variable of the current object i.e obj and studentId simply refers to the local variable. This avoids naming conflict.

OUTPUT-

Student Id: 2

Other usage of ‘this’ keyword –

1. this can be used to refer to the current class instance variable.(As discussed above)

2. this can be used to invoke current class method (implicitly).

class Abc {
  protected int studentId; // instance variable (class variable)
  public   Abc(int id) {  // local variable (parametric variable)
  studentId = id;// assign value of local variable to instance variable
  this.showData();  // implicit call 
}
 public void showData() {  
 System.out.println("Student Id: " +  studentId);
 }
 public static void main(String args[]) {
 Abc obj = new Abc(2);
}
}

OUTPUT-

Student Id: 2

3. this() can be used to invoke current class constructor.

class Abc {
  protected int studentId; // instance variable (class variable)
  public Abc() {
  System.out.println("In non-parameterized constructor");
} 
public Abc(int id) {   // local variable (parametric variable) 
this();             // reusing constructor
studentId = id; // assign value of local variable to instance variable
this.showData();   // implicit call
}
public void showData() {  
System.out.println("Student Id: " + studentId);
}
public static void main(String args[]) { 
Abc obj = new Abc(2);
}
}

OUTPUT-

In non-parameterized constructor

Student Id: 2

4. this can be passed as an argument in the method call.

class Abc {
  protected int studentId; // instance variable (class variable)
public Abc(int id) {  // local variable (parametric variable)         studentId = id; // assign value of local variable to instance variable
}
public void showData(Abc obj) {  
System.out.println("Student Id: " + studentId);
}
public void m() {
showData(this);
}
public static void main(String args[]) {
Abc obj = new Abc(2);obj.m();
}
}

OUTPUT-

Student Id: 2

5. this can be passed as an argument in the constructor call.

6. this can be used to return the current class instance from the method.

So guys that’s all for this blog and I am sure by now most of you had got a clear idea of what is ‘this’ keyword and how to use it under required circumstances. If any of you have suggestions regarding the same, please write it in below comment section.

3 thoughts on “‘this’ Keyword in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s