Skip to main content

Posts

Showing posts from October, 2015

this Keyword in Java

The  this  keyword in Java is a reference to the current object.  It is used within an instance method or a constructor to refer to the object whose method or constructor is being called.   Primary uses of the  this  keyword: Distinguishing instance variables from local variables/parameters:   When a method or constructor parameter has the same name as an instance variable,  this  is used to explicitly refer to the instance variable.  Without  this , the local variable or parameter would take precedence. Invoking a constructor from another constructor within the same class:   This is known as explicit constructor invocation.  It allows a constructor to reuse the initialization logic of another constructor in the same class. Referring to the current object in general:   While less common in simple scenarios,  this  can be used to pass the current object as an argument to a method, or to return the curren...