17) this keyword
What is this
"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:
Using ‘this’ keyword to refer current class instance variables
Using this() to invoke current class constructor
Using ‘this’ keyword to invoke current class method
Using ‘this’ keyword as method parameter
Using ‘this’ keyword to return the current class instance
Using ‘this’ keyword as an argument in the constructor call
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.
Example of Variable Hiding
Output of the above program
Value of variable :10
Value of variable :40
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
Value of Instance variable :5
Value of Local variable :10
Value of Instance variable :5
Value of Local variable :40
1. 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.
Example of this with Constructor
Output of above program
Inside Constructor with String parameter as Test
Inside Constructor without parameter
As you can see “this” can be used to invoke an overloaded constructor in the same Class.
2. 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
Inside Method TWO
Inside Method ONE
3. this keyword as Method parameter
4. Using ‘this’ keyword to return the current class instance
Output:
a = 10 b = 20
5. Using ‘this’ keyword as an argument in the constructor call
Note*:
this keyword can only be the first statement in Constructor.
A constructor can have either this or super keyword but not both.
Last updated
Was this helpful?