'this'keyword refers to the current Object (-the object whose method or constructor is being called), the most common use of 'this' keyword is to remove ambiguity when a method level variable is hiding a class level variable.
Usage of this keyword:
- 'this' keyword in Java is used to refer to current Object/Instance within an instance method or constructor
- t can be used to access Instance variable within a method or constructor
- It can be used to call one constructor from another one
- It can be used to pass current Object/Instance as parameter in a method
- It can be used to return current object from a method
this keyword with Field (Instance variable):
This is the most common use of 'this' keyword, to remove ambiguity when a method level variable is hiding a class level variable.
package com.meritmix.blog.java;
public class ThisKeyword {
private int a=10;
public void print(int a) {
// now method level variable/variable passed in parameter hides variable 'a' at class level
System.out.println("print loal variable: "+a);
System.out.println("print class level variable: "+this.a);
}
public static void main(String[] args) {
new ThisKeyword().print(5);
}
}
Output:
print loal variable: 5 print class level variable: 10
this keyword with Constructor:
'this' keyword can be used to call another overloaded constructor from a constructor of an object. This is called explicit constructor invocation. This occurs when class has one or more overloaded constructor.
package com.meritmix.blog.java;
public class ThisKeywordConstructor {
public ThisKeywordConstructor() {
this(5);
System.out.println("invoke parameterized constructor(int version) from defauld constructor");
//this(5); Error: Constructor call must be the first statement in a constructor
}
public ThisKeywordConstructor(int a) {
System.out.println("int :"+a);
}
public ThisKeywordConstructor(String str) {
this();
System.out.println("invoke defauld constructor from parameterized constructor(String version)");
}
public static void main(String[] args) {
ThisKeywordConstructor obj = new ThisKeywordConstructor("test");//invoke String version
}
}
Output:
int :5 invoke parameterized constructor(int version) from defauld constructor invoke defauld constructor from parameterized constructor(String version)
Note:
- 'this' keyword can only be the first statement in Constructor
- A constructor can have either 'this' or 'super' keyword but not both
this keyword with method:
'this' keyword can be used to call another method from a method of of same class.
Example:
package com.meritmix.blog.java;
public class ThisKeywordMethod {
public ThisKeywordMethod() {
}
public void demo() {
System.out.println("demo method");
this.print(5);// this is same as calling print(5)
}
public void print(int a) {
System.out.println("value: "+a);
}
public static void main(String[] args) {
ThisKeywordMethod obj = new ThisKeywordMethod();//invoke String version
obj.demo();
}
}
Output:
demo method value: 5
this keyword as parameter:
package com.meritmix.blog.java;
public class ThisAsMehodParameter {
int a=0;
public void print() {
print(this);
}
public void print(ThisAsMehodParameter obj) {
System.out.println(obj.a);
}
public static void main(String[] args) {
ThisAsMehodParameter obj =new ThisAsMehodParameter();
obj.a =20;
obj.print();
}
}
this keyword as return type:
package com.meritmix.blog.java;
public class ThisAsMehodReturnType {
int a=0;
public ThisAsMehodReturnType getObject() {
return this; //return current object
}
public void print() {
System.out.println(getObject().a);
}
public static void main(String[] args) {
ThisAsMehodReturnType obj =new ThisAsMehodReturnType();
obj.print();
}
}
No comments:
Post a Comment