this keyword in Java

This entry is part 16 of 34 in the series Core Java Course

What is this

this  is a keyword in Java. It can be used inside the Method or constructor of  Class. It(this) works as a reference to the current Object whose Method or constructor is being invoked. The this keyword can be used to refer to any member of the current object from within an instance Method or a constructor.

this keyword with field(Instance Variable)

this keyword can be very useful in the handling of Variable Hiding. We can not create two instance/local variables with the same name. However it is legal to create one instance variable & one local variable or Method parameter with the same name. In this scenario the local variable will hide the instance variable this is called Variable Hiding.

Example of Variable Hiding

Output of the above programme

As you can see in the example above the instance variable is hiding and the value of the local variable(or Method Parameter) is displayed not instance variable. To solve this problem use this keyword with a field to point to the instance variable instead of the local variable.

Example of this keyword in Java for Variable Hiding

Output of the above program

 

this Keyword with Constructor

“this” keyword can be used inside the constructor to call another overloaded constructor in the same Class. This is called the Explicit Constructor Invocation. This occurs if a Class has two overloaded constructors, one without argument and another with argument. Then the this” keyword can be used to call constructor with argument from the constructor without argument. This is required as the constructor can not be called explicitly.

Example of this with Constructor

Output of above program

As you can see “this” can be used to invoke an overloaded constructor in the same Class.

Note*:

  • this keyword can only be the first statement in Constructor.
  • A constructor can have either this or super keyword but not both.

this Keyword with Method

this keyword can also be used inside Methods to call another Method from same Class.

Example of this keyword with Method

Output of above program

 

Example of this keyword as Method parameter

Series Navigation<< Different ways to create an object in JavaJava Static Keyword >>

43 Comments this keyword in Java

    1. admin

      Hi Jenith,

      I have corrected the output of given program(this keyword for constructor).
      Thanks for pointing out this problem. If you find more problems do let me know.

      Thanks

      Reply
  1. DannyS

    Scoured the internet to get clarification on this (pun intended). The light bulb finally went on. Thanks.

    Reply
  2. Abhishek

    the given example this keyword is not best use in method level because without this you can call method…………………………………………………. so please give me best example of this keyword in method level………

    Reply
    1. Vivekanand Gautam

      Hi Abhishek,

      I am agree with you given example is not best example for this @Method level. But i couldn’t think of any other example for the same. Meanwhile i have added an example for Method parameter. WHich might be helpful to you. Would update the article in future in case i get any example.

      Thanks

      Reply
  3. ram

    i need a answer for following program
    write a java program to implement the usage of “this” keyword for the following cases
    1. to refer the current class instance variable.
    2. to invoke the current class constructor
    3. to invoke the current class method
    4. to return current class instance
    i need all in one programs with simple codeing…….
    i am waiting …!

    Reply
    1. Prabhakar

      import java.util.Scanner;

      class Bank
      {
      int accno;
      String name;
      int bal;

      Bank(int accno,String name)
      {
      this.accno=accno;
      this.name=name;

      }
      Bank(int accno,String name,int bal)
      {
      this(accno,name);
      this.bal=bal;
      }

      public void show_details()
      {
      System.out.println(“Accno=”+accno);
      System.out.println(“Name=”+name);
      System.out.println(“Balance=”+bal);
      }
      public void deposit()
      {
      System.out.println(“Enter Amount to be deposit”);
      Scanner sc=new Scanner(System.in);
      int amount=sc.nextInt();
      bal=bal+amount;
      show_details();
      }
      }
      public class Ex_this
      {

      public static void main(String[] args) {
      Bank b1=new Bank(1001,”Hari”,1000);
      b1.show_details();
      b1.deposit();

      }

      }

      Reply
  4. Hash

    Can you please give the output for the last Example of this keyword as Method parameter. I need only this output for understanding

    Reply
  5. Hena Girl

    If some one needs to be updated with most recent technologies afterward he must be visit this website and be up to date daily.

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *