this keyword in java
In this article we will learn about this keyword in java.
- By using this keyword, you can refer any member (hidden variables, data fields, constructors, methods) of the current object.
- A very genuine reason to use this keyword in java because a field is hidden or shadowed by the another constructor or method parameter
- In other words you can say, this keyword is used to highlight the hidden data fields of an object. Utilizing the similar name of method or contructor parameters hides the current object data fields
Example 1: Using this keyword with a hidden data field
Example 2:Using this keyword with a contructor
using this keyword for data members
using this keyword with constructor
Example 3: Using this keyword with a member method
Calling member method with this keyword