This Keyword in Java

 

In Java programming language the keyword ‘this’ is used very commonly as a reference variable to refer to the current object.

 

Usage of Java ‘this’ keyword:

The following are some of the uses of the ‘this’ keyword in Java programming language:

  1. In java programming language the ‘this’ keyword is used to refer to the instance variable of the current class.
  2. In java programming language the ‘this’ keyword is used to call or invoke the constructor of the current class.
  3. In java programming language the ‘this’ keyword is also used to call or invoke any method of the current class implicitly.
  4. In java programming language the ‘this’ keyword can be used as an argument when a method is called.
  5. The ‘this’ keyword in java programming language can also be used as an argument to call the constructor.
  6. The ‘this’ keyword can also be used to return the instance of the current class in java programming language.

The ‘this’ keyword can also be used as a reference variable that defines the state and behavior of an object in the class.

 

The ‘this’ keyword can be used to refer current class instance variable:

In java programming language if there is any confusion between a parameter of a method and the instance variable of the current class then the ‘this’ keyword can solve this problem.

 

Understanding the problem without this keyword:

Consider the following example in which it is demonstrated that what will happen if we do not use the ‘this’ keyword:

CODE:

class Employee {

int no;

string name;

Employee (int no, string name) {

no = no;

name = name;

}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee p1 = new Employee (121, “Stuart”);

Employee p2 = new Employee (122, “Bill”);

p1. display ();

p2. display ();

}

}

OUTPUT:

0 null

0 null

In the above example, a class named ‘Employee’ is declared that has two data members and one method or member function and a parameterized constructor.

In the code above, we got 0 and null as output because the parameters or arguments of the constructor and the instance variables of the class were same. To overcome this problem the ‘this’ keyword is used to differentiate between the instance variables and the local variables or parameters.

 

Solution of the above problem by ‘this’ keyword:

Consider the following example in which we have used the ‘this’ keyword to overcome the problem created in the above example:

CODE:

class Employee {

int no;

string name;

Employee (int no, string name) {

this. no = no;

this. name = name;

}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee p1 = new Employee (121, “Stuart”);

Employee p2 = new Employee (122, “Bill”);

p1. display ();

p2. display ();

}

}

OUTPUT:

121 Stuart

122 Bill

In the above example, a class named ‘Employee’ is declared that has two data members and one method or member function and a parameterized constructor.

In the code above, we did not get 0 and null as output because the parameters or arguments of the constructor and the instance variables of the class were same but we used the ‘this’ keyword with variables.

If the local variables and the instance variables in a class are different then there is no need of using the ‘this’ in the program, without it we will get the required output.

 

Program where ‘this’ keyword is not required:

Consider the following example in which the local and the instance variables are different hence we do not need the ‘this’ keyword:

CODE:

class Employee {

int no;

string name;

Employee (int n, string n1) {

no = n;

name = n1;

}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee p1 = new Employee (121, “Stuart”);

Employee p2 = new Employee (122, “Bill”);

p1. display ();

p2. display ();

}

}

OUTPUT:

121 Stuart

122 Bill

In the above example, a class named ‘Employee’ is declared that has two data members and one method or member function and a parameterized constructor. The record will be passed from the main function by using the objects as constructor arguments that is n and n1. Here n is the number of the employee and n1 is the name of the employee. These arguments will be assigned to the data members then in the parameterized constructor.

Then comes the method that is display () which is used to display the record of the employees. In the main function we created two objects to enter the records first and then to display the record. The objects get the memory in the heap area and the reference variables refer to the objects allocated in the heap memory. In the above example both e1 and e2 are the reference variables that refer to the objects allocated in memory.

 

‘this’ can be used to invoke current class constructor:

In Java programming language, the ‘this’ keyword can be used to call the constructor of the current class. We use ‘this’ keyword when we have a number of constructors and want to reuse them.

Consider the following example in which we have used the ‘this’ keyword to call a constructor:

CODE:

class Employee {

int no;

string name;

Employee () {

system. out. println (“Default constructor is called”);

}

Employee (int no, string name) {

this ();

this. no = no;

this. name = name;

}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee p1 = new Employee (121, “Stuart”);

Employee p2 = new Employee (122, “Bill”);

p1. display ();

p2. display ();

}

}

OUTPUT:

Default constructor is called

Default constructor is called

121 Stuart

122 Bill

In the above example, the default constructor is invoked two times once when we used the “this ()” statement and once when object is created. We can see that the constructor can be called by using the ‘this ()’ statement a number of times

 

Where to use ‘this ()’ constructor call?

In Java programming language the ‘this ()’ calling of the constructor is used to reuse the constructor in a constructor. The ‘this ()’ calling of the constructor is used to maintain the chain between the constructors. In other words we can say that it can be used for constructor chaining.

Consider the following example in which we have demonstrated this:

CODE:

class Employee {

int no;

String name;

String city;

Employee (int no, string name) {

this. no = no;

this. name = name;

}

Employee (int no, string name, String city) {

this (no, name);

this. city = city;

}

void display () {

system. out. println (no + “ ” + name + “ ” + city);

}

public static void main (sting args []) {

Employee p1 = new Employee (121, “Stuart”);

Employee p2 = new Employee (122, “Bill”, “Quebec”);

p1. display ();

p2. display ();

}

}

OUTPUT:

121 Stuart null

122 Bill Quebec

In the above example we used two parameterized constructors are used and through the ‘this’ keyword we initialized our variables. In the second constructor there are three parameters. As we have initialized the first two variables in the first constructor therefore, there is no need of initializing them in the second constructor. When we created the first object, we passed two arguments through which in the output we got a null for the city. In the second parameterized constructor there are three parameters including city and hence in the output we got all the three values.

It can be seen that first constructor is called or invoked in the second constructor by using the keyword ‘this’ as ‘this (no, name)’. In this way we have created a chain each time when the second parameterized constructor will be called the first will be automatically called. And we call it as chaining constructor.

It should be noted here that the statement ‘this ()’ must be the first statement in the constructor to call the other constructor. If this statement is not the first statement then an error will be occurred by the java compiler.

Consider the following example in which we have not used the ‘this ()’ calling constructor as the first statement in the constructor:

CODE:

class Employee {

int no;

string name;

Employee () {

system. out. println (“Default constructor is called”);

}

Employee (int no, string name) {

this. no = no;

this. name = name;

this ();

}

void display () {

system. out. println (no + “ ” + name);

}

public static void main (sting args []) {

Employee p1 = new Employee (121, “Stuart”);

Employee p2 = new Employee (122, “Bill”);

p1. display ();

p2. display ();

}

}

OUTPUT:

Compile time error

In the above example we used the ‘this ()’ statement or constructor calling not as the first statement in the constructor and hence we got an error from the compiler.

 

The ‘this’ keyword can be used to invoke current class method (implicitly):

In Java programming language we can use the ‘this’ keyword to call or to invoke the method or function of any class. And we do not use the ‘this’ keyword then the compiler of java programming language automatically adds the ‘this’ keyword in the program when the function or method of the class is invoked or called.

Consider the following example in which we have used the ‘this’ keyword to invoke a method of the current class:

CODE:

class B {

void x () {

system. out. println (“Method x has invoked”);

}

void y () {

this. x ();

}

void z () {

y ();

}

public static void main (String args []) {

B b1 = new B ();

b1. z ();

}

}

OUTPUT:

Method x has invoked

In the above example a class ‘B’ has declared which has three methods. The methods of this class calls other methods by using the ‘this’ keyword and if the ‘this’ keyword is not used then the java compiler will add the ‘this’ keyword to call or invoke the method.

In the main function an object of the class has been created which calls the method z (). When the control is transferred to method z (), it calls the method y () without using the ‘this’ keyword and compiler will add itself. Then in the method y () we used the ‘this’ keyword to call or invoke the function or method x and in the method x there is a print statement that prints that the method x is invoked.


The ‘this’ keyword can be passed as an argument in the method:

In java programming language, we can also use or pass the ‘this’ keyword as an argument in the method or function of the current class. The ‘this’ keyword in java is mainly used in event handling.

Consider the following example in which we have passed the ‘this’ keyword as an argument to a method of the current class:

CODE:

class Q {

void x (Q obj) {

system. out. println (“The x method of current class is invoked”);

}

void y () {

x (this);

}

public static void main (string args []) {

Q  q1 = new Q ();

q1. y ();

}

}

OUTPUT:

The x method of current class is invoked

In the above example, we used the ‘this’ keyword to be passed as an argument to a method to invoke that method. As in the above code there is no default constructor, hence a default constructor will be created by the compiler when the object is create. After creating the object we called the method y () using that object. In this method the method x () is invoked by passing the ‘this’ keyword as an argument. When the control is transferred to the method x (), then the message will be printed that “The method x of current class is invoked”. This shows that the ‘this’ keyword successfully invoked the method of current class when it was used as an argument to be passed for that method.

Application of ‘this’ that can be passed as an argument:

In Java programming language the ‘this’ keyword is usually used as an argument for the current method of the class in event handling. t can also be used when we have to provide the reference of one class to the other class.

The ‘this’ keyword can be passed as argument in the constructor call:

In java programming language, the ‘this’ keyword can also be passed in the constructor. The ‘this’ keyword is passed to constructor when we have to use only one object in a number of classes or in multiple classes.

Consider the following example in which we have used the ‘this’ keyword to pass to the constructor when the object is created:

CODE:

class C {

D obj;

C (D obj) {

this. obj = obj;

}

void display () {

system. out. println (obj. x);

}

}

class D {

int x = 5;

D () {

C c1 = new C (this);

c1. display ();

}

public static void main (string args []) {

D d1 = new D ();

}

}

OUTPUT:

5

In the above example, we have declared two classes named C and D. In the code above we used the ‘this’ keyword to pass to the constructor. This was done when the object of the class was created, in the class name we just passed the ‘this’ keyword in the following statement:

C c1 = new C (this);

The ‘this’ keyword can be used to return current class instance:

In java programming language, the ‘this’ keyword can be returned as a statement from the method of the current class. When we are returning the ‘this’ keyword as a statement from the method then the return type of that method should be the class type that is non primitive type.

Consider the following example in which we have shown the syntax of how the ‘this’ keyword can e returned as a statement:

CODE:

return type <name of the method> () {

return this;

}

Example of the ‘this’ keyword that you return as a statement from the method:

Consider the following example in which we have returned the ‘this’ keyword as a statement from the method of the current class:

CODE:

class B {

B getB () {

return this;

}

void display () {

system. out. pintln (“Hello world”);

}

class C {

public static void main (string args []) {

new B (). getB (). display ();

}

}

OUTPUT:

Hello world

In the above example, two classes have been declared. In the second class the main function or method is defined. The ‘this’ keyword is returned as statement in the method getB () of the class B. This method is called in the main function and in the class C.

Proving ‘this’ keyword:

Consider the following example in which we have proved that the ‘this’ keyword in java programming language refers to the instance of the current class:

CODE:

class B {

void x () {

system. out. println (this);

}

public static void main (string args []) {

B obj = new B ();

system. out. println (obj);

obj. x ();

}

}

OUTPUT:

B@4385dfg97

B@4385dfg97

In the above example we are printing the reference variable and the ‘this’ keyword. A class name B has created which has a method that is x () which prints the ‘this’ using print statement. Then in the main method or function, the object of the class B has been created. This object is also printed first and then is used to call the method of the current class that is B. It can be seen in the above example that the output for both of the reference variables is same that is when the ‘this’ keyword was used and when the object was printed.