Java this keyword

java this keyword Java this keyword

Basically Java “this” keyword refers to the current object. “this” is the reference variable which is holding the object of current class. In this tutorial, we will discuss, why we use “this” keyword in our application and how it can be used.

To understand “this” keyword better let’s take a real world example.

When you call someone and someone on different side ask you “who is this”, you say “This is me / This is your_name”. What actually is happening here, you are referring to yourself, you are the actual object. You are referring yourself with the keyword “this”. 

Same thing is happening in java, this keyword is used to refer to the current object. 

Some important things to remember about “this” keyword
  1. this can be used to access data members of current class.
  2. this can be used to access methods of the class.
  3. this can also invoke the argument or non arguments constructor (We will discuss about constructors later) of current class.
  4. this is holding the reference of current class object, then it can be used to with the method to return the current
  5. class object to caller.
  6. this can not be used outside of the class.

Let’s see the example explain below :

java this keyword
java this keyword

Leave a Reply

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