Java this Keyword:

  • this is a keyword in Java and is used as a reference variable which refers to the current object.
  • It can be used within the law or the constructor of class.
  • Works as a reference to the current object whose method or constructor is being used.
  • Java this keyword can be used to reference any member of the existing object from within an example method or constructor.




Note: Here it is used to start the member of the current object. As such, this.width refers to the variable width of the current object, which has implemented the constructor. Width refers to the parameter only found in the constructor.


this keyword instance variable:

  • this keyword can be very useful in managing hiding in java this keyword.
  • We can not create two examples / local variables with the same name.
  • Although it is legal to create an example variable with a variable or a local variable or the same name.


this keyword with the constructor:

  • The keyword “this” can be used within the constructor, which can be called an overloaded constructor call in the same class.
  • This happens when Class 2 is overloaded constructs, with the second argument without argument, the keyword “this” can then be used to call the constructor without reasoning from the constructor with reasoning.


this keyword with the method:

  • this keyword can also be used within methods to call any other method from the same class.




Note: In the above example, parameter (formal arguments) and instance variables are same that is why we are using “this” keyword to distinguish between local variable and instance variable.