this keyword in java refers to current object,
where to use this keyword in java ?
this keyword in java having multiple benefits and usage ,lets see one by one :
1.To Refer Current class instance variables we can use this keyword in java
Example :
step 1: firstly create a class Website.java as below
class Website{
String authorName;
String websiteUrl;
public Website(String authorName, String websiteUrl) {
this.authorName = authorName;
this.websiteUrl = websiteUrl;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getWebsiteUrl() {
return websiteUrl;
}
public void setWebsiteUrl(String websiteUrl) {
this.websiteUrl = websiteUrl;
}
@Override
public String toString() {
return "Website [authorName=" + authorName + ", websiteUrl=" + websiteUrl + "]";
}
}
Step 2 : create a class WebsiteCreator.java as below :
public class WebsiteCreator { public static void main(String[] args) { Website website1 =new Website("Swapnil V","https://www.crtr4u.com"); Website website2 =new Website("Prashant V","https://www.gymbag4u.com"); System.out.println("Website Details :"); System.out.println(website1); System.out.println(website2); } }
When we run above program we will get output as below :
Website Details : Website [authorName=Swapnil V, websiteUrl=https://www.crtr4u.com] Website [authorName=Prashant V, websiteUrl=https://www.gymbag4u.com]
Explanation of above program : when we are creating object of Website class by using new keyword as below :
Website website1 =new Website("Swapnil V","https://www.crtr4u.com");
we are passing name "Swapnil V" and url "https://www.crtr4u.com" as input to the constructor of Website class ,inside Website class constructor we are using this keyword to assign values to instance variables of class as below : public Website(String authorName, String websiteUrl) {
this.authorName = authorName;
this.websiteUrl = websiteUrl;
}
using this keyword we are referring class instance variables inside constructor and setting values which are received.2.To invoke current class constructor we can use this() in java :
to invoke current class constructor we can use this() in java , it is used for constructor chaining ,lets see the real time use of this() in java :
Example :
create a class AdditionOperations.java as below : with two constructors :
public class AdditionOperations {
int num1,num2,num3;
public AdditionOperations(int num1,int num2) {
this.num1=num1;
this.num2=num2;
}
public AdditionOperations(int num1, int num2, int num3) {
/*this is constructor chaining
here we are calling constructor with two arguments using this()*/
this(num1,num2);
this.num3 = num3;
}
void add() {
System.out.println("Addition is "+(this.num1+this.num2+this.num3));
}
}
create a class ConstructorChaining.java as below :
public class ConstructorChaining {
public static void main(String[] args) {
AdditionOperations op1=new AdditionOperations(1,2);
op1.add();
AdditionOperations op2=new AdditionOperations(4,3,5);
op2.add();
}
}
output of above code is :
Addition is 12
3.Calling current class method using this keyword in java:
you can invoke method by using this keyword, note that ,if you don't use this keyword then compiler automatically add this keyword.
Example :
public class CallConstructor {
public static void main(String[] args) {
WellWisher wellWish=new WellWisher();
}
}
class WellWisher{
WellWisher(){
System.out.println("welcome to Java");
this.msgDisplay("called msgDisplay method using this keyword");
}
public void msgDisplay(String message){
System.out.println(message);
}
}
Thanks a lot for reading our article ,keep programming.