java2db

How to work with this keyword in java



The this keyword differentiates the local and instance variables and methods which have the same name.The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter .

this keyword in java this keyword is a reference to the current object.
Constructor call using this (ex:this() )should be in the first line of the called constructor.
this keyword in java Possible to call the current class methods by using this keyword (Ex: this.methodxx()).

 

this keyword in java  If the instance variable and the local variable having the same name , then we can use like below.
this keyword in java

The output is 5.

 

 

 Method calling with the keyword this .
this keyword in java

 
this keyword in java Constructor calling with the keyword  this :
this keyword in java

 We cannot use the keyword this inside a static method .

 

Example for this keyword :

Line number 8 to 11 ,calling zero argument constructor and also assigning local parameters to instance variables using the this keyword.

Execution Result :

Without using the this keyword :

See the code below for better understanding , which will give the same result . But this time we are not using this keyword.




Related Posts :
How to use super keyword in java with an example
   [will not be published]



^ <