☕ Java
Beginner
What is the this keyword in Java?
Answer
The this keyword is a reference to the current object — the instance on which the method or constructor is being called. It is used to resolve ambiguity when instance field names clash with parameter names: this.name = name;. It is also used to call another constructor in the same class (constructor chaining): this(defaultValue); — must be the first statement. You can pass this as an argument to other methods. In anonymous classes or lambdas, this refers to the enclosing class instance, not the anonymous class. Static methods do not have access to this because they are not called on instances.