Engineering Full Stack Apps with Java and JavaScript
The “this keyword” in Java refer to the the current object (current instance). All your instance variables may be referred as 'this.variableName' even though it is implicit in most cases.
The “this keyword” is mostly useful in below cases:
Refer to instance variables of a class when a local variable has precedence over it.
A local variable with same name as the instance variable will take precedence over the instance variable in the local variable’s scope. We can use this keyword in such cases to refer to the instance variable. A major use of this approach comes in the setter methods.
We can call one constructor from another constructor using this().
Similar to methods, we can overload constructors i.e. we can have more than one constructor with same name and but different parameter set. Note that constructor names for a class are always same as it is the class name itself.
Java won’t allow us to create an infinite loop by calling one constructor from another and then the other calling it back. Compilation will fail because of “Recursive constructor invocation”.
Similarly you may call a super class constructor using super() keyword (we will discuss about super later.)
To refer to the current object and pass it to a method or return it from a method
Someone might want to get the reference to the current object.
You may also register yourself (current object) as a listener in some callback register.
Important Note
Since this object represent the current object in question (whichever object user created using new), it cannot be used from a static context.
However, we can refer to a static variable using a this pointer (from an instance context.)
Example: Refering to instance variables of a class when a local variable has precedence over it.
class MyClass{
int myVar=5;
public void setMyVar(int myVar)
{
this.myVar = myVar;
}
}
Example: Calling one constructor from another constructor using this().
class MyClass{
int myVar=5;
public void setMyVar(int myVar)
{
this.myVar = myVar;
}
}
Example 3: Refering to the current object and pass it to a method or return it from a method.
We can invoke a method with signature myMethod(MyClass c) from MyClass as:
myMethod(this);
We can return the current object from a method with return type MyClass from MyClass as:
public MyClass getInstance()
{
...
return this;
}
Comments
this keyword
please explain this keyword in more detail. if one class have 2 object . then "this" keyword refer whom and in case of parent and child class use of "this" keyword refer whom super or child class or object.
this will refer to current
this will refer to current object or instance.
if you have an instance variable, then every object created will have its own copy of the instance variable. Similarly there will be different copy of this object.
if there are three objects, there will be three instance varialbe copies and three this copies.
this is mainly used to differentiate between an instance and local variable with same name as in example 1. it is also used for calling another constructor.
for calling super class constructor, you use another keyword super.
Use 3: To refer to the
Use 3: To refer to the current object and pass it to a method or return it from a method.
need example for this
this for static context
How does "this" behave in static context . I mean if There is a static block, and a non static method, can a static method call other non-static methods in the same class by using the 'this' , and also is vice versa possible?
"this" refer to the current
"this" refer to the current object. It will be different for each object that you create. If you refer to "this" from a static block or method, then Java will not be able to decide which of the objects to call. So you cannot call this from static context. In simple, you cannot call or access anything belonging to instance context from a static context.