We will see the usage of “this” keyword one by one with examples –
- “this” keyword refers to current class instance variables
When local variable and parameter variable are of same name, then “this” keyword is used to distinguish in between them.
local / parameterised variable hides the instance variable values called as variable hiding.
Example (same variable name for local and instance variable) –
package dis; public class Usage1 { int id = 10; void dispData(int id){ System.out.println("Parameterised variable id: " + id ); System.out.println("Instance variable id 'using this': " + this.id); //parameterised / local variable hides the instance variable values - variable hiding. this.id = id; System.out.println("variable id 'without using this': " + id); } public static void main(String[] args) { Usage1 usg = new Usage1(); usg.dispData(50); } }
out put:
Parameterised variable id: 50
Instance variable id ‘using this’: 10
variable id ‘without using this’: 50
id (value 50) refers to local or parameterised variable inside dispData() method, but this.id (value 10) refers to the instance variable.
NOTE: “this” keyword is a non static variable, we can not use “this” keyword in static context (in static methods and should not use for static members).
Example (different variable name for local and instance variable) –
no problem of distinguishing in between them and also no use of “this” is required to access them
package dis; public class Usage2 { int i = 10; void dispData(int id){ System.out.println("Parameterised variable id: " + id ); System.out.println("Instance variable i: " + i); } public static void main(String[] args) { Usage2 usg = new Usage2(); usg.dispData(50); } }
output :
Parameterised variable id: 50
Instance variable i: 10
- “this” keyword is used to call current class methods.
package dis; public class Usage3 { void callDisplay(){ this.Display(); //same as Display(); //compiler automatically adds this, and calls this.Display() } void Display(){ System.out.println("output from Display()"); } public static void main(String[] args) { Usage3 usg = new Usage3(); usg.callDisplay(); } }
- this() method is used to invoke current class constructor and this(“arg1”..) for parameterised constructor.
NOTE: invoking constructor by using this() should be the 1st statement.
package dis; public class DisConstructor { int id; public DisConstructor() { System.out.println("default constructor"); } public DisConstructor(int id){ this(); //same as DisConstructor(); this.id = id; System.out.println("id: "+id); //this(); //compiler error } public static void main(String[] args) { DisConstructor dc = new DisConstructor(15); //invokes parameterised constructor } }
Output:
default constructor id: 15
parameterised this(arg1…) call –
package dis; public class DisConstructor { int id; public DisConstructor() { this(12); } public DisConstructor(int id){ System.out.println("id: "+id); } public static void main(String[] args) { DisConstructor dc = new DisConstructor(); //invokes default constructor } }
output :
id: 12
NOTE: A constructor can either have this or super, but not both.
- this keyword as an argument to method
package dis; public class DisAsArgument { DisAsArgument(){ Display(this); } void Display(DisAsArgument daa){ System.out.println("test Display()"); } public static void main(String[] args) { DisAsArgument da = new DisAsArgument(); } }
output:
test Display()
- return this statement
“return this;” is used to return current class instance, so the return type should be class type (not of any primitive type like int, string, void etc…)
public class Returndis { public static void main(String[] args) { SubClass sc = new SubClass(); sc.inSubClass().Display(); } } class SubClass{ int id; SubClass inSubClass(){ //inSubClass have return type as 'SubClass' id = 10; return this; } void Display(){ System.out.println("Class - SubClass, id = " + id); } }
Short link – bit.ly/qav-thiskeyword