this Keyword in Java with Examples
this Keyword in Java The this keyword in Java is a reference variable that refers to the current object of the class that is currently executing. In simple words, this represents the current memory address of the object. ✅ Definition this refers to the current object It is used inside constructors, methods, and instance blocks It is explicitly written by the programmer The compiler does not create this implicitly It helps differentiate between instance variables and local variables when they have the same name 🌍 Real-Life Example Classroom Example A teacher says: “This student should come forward” The word “this” refers to the current student being addressed Similarly, in Java, this refers to the current object. 🔹 Uses of this Keyword in Java 1. Differentiating Instance Variables and Local Variables When local variables and instance variables have the same name, this is used to avoid confusion. class Student { int id; Student(int id) { ...