This Keyword in Java

This keyword in Java

This keyword in Java Java is an object-oriented and class-based language developed by Sun Microsystems. It has a diverse range of keywords that are used to perform predefined or some internal processes in the code. We cannot use these keywords as variables, classes, objects, or any other type of classifiers. The programmer can simply use the keyword and perform the needed action.  There are approximately 51 keywords in java language. Some of the most useful ones are abstract, break, case, char, class, continue, float, for, if, null, private, protected, try, this, void, while etc. Out of these 51, 2 keywords are reserved that means for future use: β€˜const’ and β€˜goto’.

One of these keywords in Java is β€˜This’ keyword. When we initialize an object in the class, this keyword helps to refer to that particular object of the class whose method is being called by the coder. This keyword can be used either inside the class of the program or in the constructor. This() can invoke the constructor of the current class being worked on which is then passed as an argument in the method call or in the constructor call.

This keyword has the following properties:

  • This keyword is used to get the current object in the program P.
  • The keyword is used to invoke the current method of the object.
  • This keyword is used to return the current object from the method.
  • This() can be invoked in current class constructor C.
  • This keyword can also be passed as a parameter to the constructor C.
  • The keyword can be passed as an argument in the constructor call.
  • This keyword eliminates the chance of naming conflicts in a constructor or method for the object.
public class ClassX{		//Initialise the class
int a;
public ClassX(int a) { 		//Constructor with a parameter
this.a = a; 
}
public static void main(String[] args) {
ClassX myObj = new ClassX(5);	//Calling the constructor 
System.out.println("Value of a = " + myObj.a);
} 
}

Output :

Value of a = 5.

Hence, the most useful property of this keyword that we understand is that it gets rid of the confusion between class parameters and attributes that too with the same name. Try rephrasing the code above without using β€˜this’ keyword. You will get the output as 0.

Example for better understanding of this keyword

class ClassA{ 		//Initialising class
    int x; 
    int y; 
ClassA (){	// Default constructor 
        x = 5; 
        y = 10; 
} 
void display(ClassA obj){	
        System.out.println("x = " +obj.x + "  y = " + obj.y); 
} 
void get(){	// Method that returns current class instance 
    display(this); 
} 
public static void main(String[] args){ 
        ClassA object = new ClassA(); 
        object.get(); 
} 
} 

Output :

x = 5  y = 10

Class: ClassA .
Instance variables: x and y.
Method get()
: To get the data for x and y.

Difference between this and super keyword in Java

As we have discussed in the previous section, β€˜this’ keyword is used to access the methods of the current class in java. However β€˜super’ keyword can access the methods of parent class in java. We can never use this keyword as an identifier as it is one of the reserved keywords in java.

This keyword can also be used for accessing static members in java. Whereas, the super keyword is majorly used to access parent class methods as well as parent class constructors. If both these keywords are used together, they will give a compile-time error. Hence, they can never be used together.

Leave a comment

Your email address will not be published. Required fields are marked *