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
- this can be used to access data members of current class.
- this can be used to access methods of the class.
- this can also invoke the argument or non arguments constructor (We will discuss about constructors later) of current class.
- this is holding the reference of current class object, then it can be used to with the method to return the current
- class object to caller.
- this can not be used outside of the class.
Let’s see the example explain below :