Skip to main content

this keyword in java

 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 3 

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.

Popular Posts

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Today we will see solution for Error In Spring Boot : ***************************  APPLICATION FAILED TO START ***************************  Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  Reason: Failed to determine a suitable driver class. --->My Application.yml : server: port: 8081 spring: datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:xe username: sys as sysdba password: root Oracle Version is 21 C Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0. check my pom.xml here : <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/m...

Stream In Java

stream in java 8 : Stream API is used to process collection of objects. stream() method is present in Collection interface from package java.util . Stream is an interface present in java.util package, filter() and map() methods are present in Stream interface. -->when to use filter()  method ? If we want to filter elements from collection based on some boolean condition then we can use filter() method.  for example  : filtering even or odd numbers from list of numbers. -->When to use map()  method ? If we want to create new object for every object in collection then we should use map() method. for example : if we want to increase every number in list by 2 , here every object is getting modified ,we have to use map() in this type of scenario. In below example , we used both filter() and map() methods on stream , inside filter method we applied some condition and in map method we are transforming our object to different one. package com.crtr4u.www; import java...

Class and Object in Java

In Java we have to always deal with objects, now let's understand what is object , how to define object in Java. What is Object In Java ? Object is an entity with STATE and Behavior. Examples of Objects : Bike ,Mobile, Student,Laptop,Car etc. If we consider Bike As Object , Bike Name is state , Bike Color is state , and Running is behavior of Bike,Starting is behavior of bike and stopping is also behavior of Bike. In Java we say , Object is an Instance of class , it means we can create object using class. What is class ? A class is template or blueprint from which objects are created. A class in java can contain fields(variables),Methods(Behaviors),blocks,constructors etc. How To Create class in Java ? Syntax To Create Class :      class College {           String collegeName="COEP";           public void getAdmission(){           System.out.println("In this method we have to wri...