this keyword in java

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

this_ex1

Example 2:Using this  keyword with a contructor

using this keyword for data members

using this keyword for data members

using this keyword with constructor

using this keyword with constructor

Example 3: Using this  keyword with a member method

Calling member method with this keyword

Calling member method with this keyword


No Responses

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.