this keyword

"this" is a keyword in Java. "this" is a predefined instance variable to hold current object reference. 'this' is also called as “Default Reference variable” of 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. this keyword can be used to refer to any member of the current object from within an instance Method or a constructor.

Rule to develop 'this':

  • this, should always be the first statement inside a constructor.

  • We cannot develop multiple this, inside a single constructor.

  • this, is used to call only constructor of the same class.

  • If we want to call constructor from another constructor we should use this. We cannot use constructor name because we can use the constructor name only at the time of object creation.

this calling statement:

'this' is used to call one constructor from another constructor of the same class in case of Constructor Overloading. Generally for 1 object creation, only 1 constructor shall be executed. But if we want to execute multiple constructor of the same class for only one object creation we can use this() calling statement.

this (parameter) will call the constructor of the same class with matching parameters.

Advantage: If we can't to use the initialization code written is another constructor of the same class, then we make use of this.

Following are the ways to use ‘this’ keyword in java:

1. Using ‘this’ keyword to refer current class instance variables

2. Using this() to invoke current class constructor

3. Using ‘this’ keyword to invoke current class method

4. Using ‘this’ keyword as method parameter

5. Using ‘this’ keyword to return the current class instance

6. Using ‘this’ keyword as an argument in the constructor call www.testingbar.com || Sujoy@testingbar.com

this keyword with field(Instance Variable) this keyword can be very useful in the handling of Variable Hiding. We cannot 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.

2. 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 cannot be called explicitly.

3.Using ‘this’ keyword to return the current class instance

4. Using ‘this’ keyword as an argument in the constructor call

Note*: www.testingbar.com || Sujoy@testingbar.com

this keyword can only be the first statement in Constructor.

A constructor can have either this or super keyword but not both.

Last updated