logo codesdope

Use of this in Java


This is a keyword in Java. It basically refers to the current object whose method or constructor is being called.

Now, let's see where this can be used.

Refer instance variable of current class


this keyword is used to refer to the instance variable of a class. This will become clearer by the following examples.
class Account{
  String name;
  int balance;
   Account(String n, int b){
    name = n;
    balance = b;
  }
  void printInfo(){
    System.out.println("Name: " + name + " Balance: " + balance);
  }
}
class This0
{
  public static void main (String[] args) {
    Account ac = new Account("Peter", 5000);
    ac.printInfo();
  }
}
Output
Name: Peter Balance: 5000
class Account{
  String name;
  int balance;
  Account(String name, int balance){
    name = name;
    balance = balance;
  }
  void printInfo(){
    System.out.println("Name: " + name + " Balance: " + balance);
  }
}
class This1
{
  public static void main (String[] args) {
    Account ac = new Account("Peter", 5000);
    ac.printInfo();
  }
}
Output
Name: null Balance: 0

In the second example, since the parameters and the data-members (instance variables) are same, therefore we need to distinguish between the two. For that, this keyword is used.

Let's see how.

class Account{
  String name;
  int balance;
  Account(String name, int balance){
    this.name = name;
    this.balance = balance;
  }
  void printInfo(){
    System.out.println("Name: " + name + " Balance: " + balance);
  }
}
class This2
{
  public static void main (String[] args) {
    Account ac = new Account("Peter", 5000);
    ac.printInfo();
  }
}
Output
Name: Peter Balance: 5000

this keyword is used to refer to the instance variables of the current class.
So in the above example by writing this.name = name, this.name refers to the instance variable whereas name on the right side of the equality operator (=) refers to the parameter value. So the value of parameter gets assigned to the instance variable.
Thus, "Peter" gets stored in the instance variable 'name' and '5000' gets stored in the instance variable 'balance'.

As argument in a method


this keyword can also be passed as an argument to some method in a class. It is used to represent an object of that class.

The following example will make it clear.

class Method{
  void method1(Method m1){
    System.out.println("This is method 1");
}
  void method2(){
    method1(this);
  }
}
class This3
{
  public static void main (String[] args) {
    Method m = new Method();
    m.method2();
  }
}
Output
This is method 1

In the above example, method1 is called inside method2. Since method1 had an object of its class as its parameter, so while calling it inside method2, we passed this keyword as its argument. So, it will pass the current instance of the class Method with which we are dealing at that time.

Return object of class


this keyword is used to represent the current object. As in the previous example, since method1 has the object of class as parameter, so when it was called inside method2, we passed this keyword as its argument in place of the object of the class.

Similarly, we can return this keyword instead of returning object of any class.

Now look at this example.

class Rectangle{
  int length, breadth;
  Rectangle(int l, int b){
    length = l;
    breadth = b;
  }
  Rectangle getObj(){
    return this;
  }
}

class This5{
  public static void main (String[] args){
    Rectangle r1 = new Rectangle(15,20);
    Rectangle r2;
    r2 = r1.getObj();
    System.out.println("length: " + r1.length + " breadth: " + r1.breadth);
    System.out.println("length: " + r2.length + " breadth: " + r2.breadth);
  }
}
Output
length: 15 breadth: 20
length: 15 breadth: 20

Here, we returned this in getObj() method.
Now, since object r1 called the 'getObj' method and this keyword represents the current object, here this keyword is used to represent the object r1. Thus by writing return this, we are returning object r1 and by writing r2 = r1.getObj(), we are assigning object r1 to object r2. Thus, the length and breadth of r2 also become 15 and 20 respectively.

Invoke current class constructor


We use this keyword to invoke a class constructor inside another class constructor.

class Account{
  String name;
  int balance;
  Account(String n){
    name = n;
    System.out.println("Happy coding");
  }
  Account(String n, int b){
    this(n);
    balance = b;
  }
  void printInfo(){
    System.out.println("Name: " + name + " Balance: " + balance);
  }
}
class This6{
  public static void main (String[] args) {
    Account ac = new Account("Peter", 5000);
    ac.printInfo();
  }
}
Output
Happy coding
Name: Peter Balance: 5000

In the above example, when we made the object ac of class Account, the constructor with two parameters was called.
The first statement inside this constructor is this(n). Here, 'this' keyword calls the constructor with the String parameter. So, when the constructor with a String and an int called this(n) ( constructor with a String ), firstly, n was assigned to name ( in the first constructor ) and then b was assigned to balance. You must have understood the rest of the code.

When we call one constructor inside another, it should be the first line of the constructor in which we are calling.

Suppose while calling the first constructor inside another we write the following code, then we will get Compile Time error.

Account(String n, int b){
  balance = b;
  this(n);
}

Invoke current class method


We can also call a method within another method of the same class.

class Method{
  void method1(){
    System.out.println("This is method 1");
  }
  void method2(){
    this.method1();
  }
}
class This7{
  public static void main (String[] args) {
    Method m = new Method();
    m.method2();
  }
}
Output
This is method 1

Here this.method1() calls the method of the current class i.e. 'Method'. Though we don't need to add this keyword to method1 since compiler does it automatically.

Let's see an example where we didn't use this keyword with method1.

class Method{
  void method1(){
    System.out.println("This is method 1");
  }
  void method2(){
    method1();
  }
}
class This8{
  public static void main (String[] args) {
    Method m = new Method();
    m.method2();
  }
}
Output
This is method 1

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.