
Play Store Application link – https://play.google.com/store/apps/details?id=com.ideepro.java25hours
Learn with Youtube video –
💡this –
![this-keyword-uses-in-java-featured-image-1280x720[1]](https://corejava25hours.com/wp-content/uploads/2021/02/this-keyword-uses-in-java-featured-image-1280x7201-1.jpg)
💡Definition-
It’s a keyword which refers to current class.
this keyword is used to-
Use of this keyword | Description | Example |
---|---|---|
this as variable | this is a reserved word in Java that refers to the current instance of the class. It can be used to access instance variables and methods of the current object. | this.x = 10; |
this as current class constructor | this can be used to call another constructor of the same class from a constructor. This is known as constructor chaining. | public MyClass() { this(0, 0); } |
this as current class method | this can be used to refer to the current object from within a non-static method of a class. It can be used to access instance variables and methods of the current object. | public void setValues(int x, int y) { this.x = x; this.y = y; } |
this as argument in method call | this can be passed as an argument to a method to indicate that the method should operate on the current object. | obj.printValues(this); |
this as argument in constructor call | this can be passed as an argument to a constructor to indicate that the constructor should create an instance of the current class. | MyClass obj = new MyClass(this); |
this as class instance | this can be used to create an instance of a class. For example, MyClass obj = new this(); creates an instance of the MyClass class. | MyClass obj = new this(); |
💡(1) Refer class variable –
package dis; public class TisVar{ String name="Kuldeep"; void show(){ System.out.println(this.name); //here } public static void main(String[] args){ TisVar tvObj = new TisVar(); tvObj.show(); } }
💡(2) Refer current class constructor-
package dis; public class TisConst{ public TisConst(){ this(5); System.out.println("Kuldeep"); } public TisConst(int age){ System.out.println("kaushik"); } public static void main(String[] args){ TisConst k=new TisConst(); } }
💡(3) Invoke current class method-
package dis; public class TisMtd{ void show(String name){ System.out.println(name); } void disp(){ this.show("kuldeep"); } public static void main(String[] args){ TisMtd tisObj = new TisMtd(); tisObj.disp(); } }
💡(4)this : passed as an argument in method call
package dis; public class TisMtdArg{ String name = "kuldeep"; void show(String name){ System.out.println(name); } void disp(){ this.show(this.name); //here } public static void main(String[] args){ TisMtdArg tisObj = new TisMtdArg(); tisObj.disp(); } }
💡(5)this : passed as an argument in constructor call
package dis; public class TisConstArg{ int age = 12; void show1(){ new TisConstArg(this.age); } public TisConstArg(int age){ System.out.println("Kuldeep - Age = "+age); } public static void main(String[] args){ TisConstArg tisConstArgObj = new TisConstArg(30); } }
💡(6)this: can be used to return the class instance
package dis; public class TisClsIns{ TisClsIns currentIns(){ return this; //here } public static void main(String[] args){ TisClsIns tisClsInsObj = new TisClsIns(); TisClsIns tisClsInsObj2 = tisClsInsObj.currentIns(); System.out.println(tisClsInsObj2); } }
💡2]:- SUPER KEYWORD:-
![super-keyword-in-java-featured-image-1280x720[1]](https://corejava25hours.com/wp-content/uploads/2021/02/super-keyword-in-java-featured-image-1280x7201-1.jpg)
💡Definition-
It’s a keyword which refers to parent class.
super keyword is used to –
Use of super keyword | Description |
---|---|
super.variable | A reference to a parent class variable. Can be used to access the value of the variable or to assign a new value to it. |
super.method() | A reference to a parent class method. Can be used to call the method from the child class. |
super() | A reference to a parent class constructor. Must be the first statement in a subclass constructor and is used to initialize the parent class before the child class. |
💡1:- Refer parent class instance variable.
package spr; class SperVar{ int age =10; } public class SuprVar extends SperVar { int age=20; void show(){ System.out.println(super.age); } public static void main(String[] args){ SuprVar suprVarObj = new SuprVar(); suprVarObj.show(); } }
💡2:- Refer parent class constructor.
package spr; class SperConst{ SperConst(){ System.out.println("parent"); } } public class SuprConst extends SperConst { SuprConst(){ super(); System.out.println("child"); } public static void main(String[] args){ SuprConst s1=new SuprConst(); } }
💡3:-Refer parent class method
package spr; <div class="line number2 index1 alt1"> class SperMtd{ void show(){ System.out.println("parent"); } } public class SuprMtd extends SperMtd{ void show(){ super.show(); System.out.println("child"); } public static void main(String[] args){ SuprMtd s1=new SuprMtd(); s1.show(); } } </div> <div class="line number15 index14 alt2"><code class="java plain"></code>
Interview Questions —
- What ‘this’ keywords refers to ?
- What ‘that’ keywords refers to ?