this Keyword in Java
๐ง What is this
?
The this
keyword is a reference to the current object in Java. It is used to avoid confusion between class attributes and method parameters when they have the same name.
๐ธ Use Cases:
-
Distinguish between instance and parameter variables.
-
Call other constructors in the same class.
-
Return current object from a method.
-
Pass current object as an argument.
๐งช Example:
๐งช Constructor Chaining using this()
:
๐ Key Points:
-
this
refers to the current class object. -
Helps resolve naming conflicts.
-
Used in method chaining and constructor chaining.