this keyword

In java, this keyword is used as a reference of the current object.

Here are few important usage of this keyword.

  1. this keyword represent current instance of the class.
  2. this can be used to call default constructor or overloaded constructor.
  3. this  can be used to solve name conflict if instance variable and local variable have the same name.
  4. this is a final variable in java.
  5. this can be passed as an arguments in constructor call.
  6. this can be passed as an arguments in method call.
  7. this can not be used in static context.
  8. this can be used to call method of current class.

 

1. this keyword represent current instance of the class:

The this keyword can be used to return the instance of the class. To prove this, we have created 2 classes Library1 and BookStore. We will return the instance of the class BookStore by using this keyword and call its method displayBook by using its instance reference named as book.

Look at the example below for better understanding.


2. this can be used to call default constructor or overloaded constructor:

The this keyword can be used to call one constructor to another constructor i.e. called constructor chaining. For calling constructor without parameter, it should be used as this() and for calling parameterized constructor it should be used as this(no. of parameters).

Note: While calling, this() must be the first statement in the constructor.

3. this  can be used to solve name conflict if instance variable and local variable have the same name:

If the name of instance variable or local variable are same. Then, there will be ambiguity to refer the desired variable name i.e. it is instance variable or local variable which we want to refer.

Let’s understand with the example below where we are invoking the constructor which is have the same name of parameters as the instance variables(bookId,bookName).

Let’s see what could be the problem if we don’t use this keyword.

Output : 0–null
0–null

Now, to resolve this issue, we use this keyword with instance variables.

Output : 123–Java book 333–C++ book

4. this is a final variable in java:

this is a final variable in java and we can not assign value to this.


5. this can be passed as an arguments in constructor call:

The this keyword can be passed in the constructor also.Let’s see with the example below.


6. this can be passed as an arguments in method call:

The this can be passed as method arguments and mostly used for event handling.


7. this can not be used in static context.

The keyword this can not be used inside static method or static initializer block. If used, will throw a compile time error.


8. this can be used to call method of current class:

The keyword this can be used to call the method of the current class. Let’s understand with the example below.

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.

 

Access modifiers
Inheritance in Java
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

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