this keyword
In java, this keyword is used as a reference of the current object.
Here are few important usage of this keyword.
- this keyword represent current instance of the class.
- this can be used to call default constructor or overloaded constructor.
- this can be used to solve name conflict if instance variable and local variable have the same name.
- this is a final variable in java.
- this can be passed as an arguments in constructor call.
- this can be passed as an arguments in method call.
- this can not be used in static context.
- 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.
public class Library1 { public static void main(String[] args) { BookStore book = new BookStore().getBook(); // will return the instance of the class bookStore book.displayBook(); } } class BookStore{ BookStore getBook(){ // this method will return return this; } public void displayBook(){ System.out.println("This is english book"); } }
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).
public class Library1 { Library1(){ System.out.println("I am getting called with this keyword"); } Library1(int a){ this(); // this will call the above constructor } public static void main(String[] args) { Library1 lib = new Library1(1); // this will invoke the constructor Library1(int a) } }
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.
public class Library1 { int bookid; String bookName; Library1(int bookid,String bookName){ bookid=bookid; bookName=bookName; } void displayBook(){ System.out.println(bookid+"--"+bookName); } public static void main(String[] args) { Library1 lib = new Library1(123,"Java book"); Library1 lib1 = new Library1(333,"C++ book"); lib.displayBook(); lib1.displayBook(); } }
0–null
Now, to resolve this issue, we use this keyword with instance variables.
public class Library1 { int bookid; String bookName; Library1(int bookid,String bookName){ this.bookid=bookid; // this keyword is used this.bookName=bookName; // this keyword is used } void displayBook(){ System.out.println(bookid+"--"+bookName); } public static void main(String[] args) { Library1 lib = new Library1(123,"Java book"); Library1 lib1 = new Library1(333,"C++ book"); lib.displayBook(); lib1.displayBook(); } }
4. this is a final variable in java:
this is a final variable in java and we can not assign value to this.
this = new bookStore(); //cannot assign value
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.
public class Library1 { BookStore bookName; Library1(BookStore bookName){ this.bookName=bookName; } void displayBook(){ System.out.println("Book is a "+bookName.book); //displaying the book from bookstore class } public static void main(String[] args) { BookStore book = new BookStore(); //invoking BookStore constructor } } class BookStore{ String book="Java book"; BookStore(){ Library1 lib = new Library1(this); lib.displayBook(); } }
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.
public class Library1 { void getBook(Library1 myBook){ System.out.println("Got the book"); } void displayBook(){ getBook(this); // passed as method arguments } public static void main(String[] args) { Library1 book = new Library1(); book.displayBook(); } }
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.
public static void main(String[] args) { this(); //compile time error Library1 book = new Library1(); }
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.
public class Library1 { Library1(){ this.displayBook(); // calling a method } void displayBook(){ System.out.println("This is a java book"); } public static void main(String[] args) { Library1 book = new Library1(); } }