PREV NEXT
Java this keyword:
- The keyword ‘this’ is used to represent current object of the class. It is used to access various elements of the class like instance variables and methods. For eg: “this.var1” and “this.method1()”
- The keyword ‘this’ when used along with parenthesis as in ‘this()’, is used to invoke the constructor of the class
- ‘this’ keyword is used to differentiate the instance variable from the local variable, when both use the same name.
- ‘this’ keyword can be passed as an argument to a method or a constructor and can even be used as a return parameter after a method execution like “return this;”
Example 1:
‘this’ used to refer the current instance variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Box { double height, width; Box(double height, double width) { this.height = height; //this.height represents the instance variable ‘height’ of the Box class this.width = width; //height,weight represents the value of the local variable given in the parameter } void display() { System.out.println("Width:"+width+"\t"+"Height:"+height); } public static void main(String args[]) Box b = new Box(3.0,5.0); b.display(); } } |
Output:
Width:5.0 Height:3.0 |
Example 2:
‘this()’ used to invoke constructor of the current class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class Box { double height, width; Box() { System.out.println(“Object created…”); } Box(double h, double w) { this(); height = h; width = w; } void display() { System.out.println("Width:"+width+"\t"+"Height:"+height); } public static void main(String args[]) Box b = new Box(3.0,5.0); } } |
Output:
Object created…
Width:5.0 Height:3.0 |
Example3:
‘this’ can be passed as an argument and can also be provided as the return value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public class Box { int width,height; Box(int a, int b) { width = a; height = b; } public void print() { display(this); //’this’ passed as an argument } public void display(Box b) { //the receiving parameter must be of the type class System.out.println("Width:"+b.width+"\t"+"Height:"+b.height); } public Box volume(){ System.out.println("volume of the box:"+(width*height)); return this; //return type of the method is of the type class } public static void main(String args[]) { Box b = new Box(5,6); Box b1 = new Box(4,5); b.print(); b1.print() b1 = b.volume(); //now b is assigned to b1, since volume() returns the reference of current object b1.print(); //b1’s instances are overwritten by b’s instances } } |
Output:
Width:5 Height:6
Width:4 Height:5 volume of the box:30 Width:5 Height:6 |
In the above program, the return type of the volume() method is Box. Thus, b.volume() returns the object reference of the class invoking it.
Hence b1 = b.volume(); logically means b1 = b;
Thus b’s instance variables are copied in b1 and hence b1.print() displays the same instances that of b.