This tutorial explains about this
keyword in java which is quite used in java programs. The tutorial explains different details of
this
keyword like what this
keyword is, use of this
keyword in method and constructor, java programs of this
keyword etc.
What is this keyword in Java
In java, this
is a keyword which holds the reference of the current object - the object whose method or constructor is being called. In other words,
this
keyword refers/holds the reference of
the same object which invoked the method or constructor.
As we know, in java an instance method or constructor is called by an object, we use this
keyword to refer the same object inside that method or constructor.
Using this
keyword we can access the
members of that object inside the method or constructor.
The keyword this
is a reference variable of non static type, it can be used inside instance method, constructor or block only. Like other non static members, it can not be
used inside static method or static block, doing so will result in compilation error. The syntax of using this
keyword is :
this
.name// To access the name instance variable of current object
this
.printDetail()// To call the printDetail method of current object
this
(String name)// To call string parameterized constructor for current object
this keyword program in Java
class
Person {String
name;int
age;public
Person(String n,int
a) {.name = n;
this
.age = a; }
this
void
printDetail() { System.out.println("Reference of this keyword = "
+this
); System.out.println("Detail using this keyword, name = "
+this
.name+" age = "
+this
.age); }public static void
main(String [] args) { Person person =new
Person("Rahul"
, 20); person.printDetail(); System.out.println("Reference of person object = "
+person); System.out.println("Detail using object, name = "
+person.name+" age = "
+person.age); Person person2 =new
Person("Pradeep"
, 22); person2.printDetail(); System.out.println("Detail using object2, name = "
+person2.name+" age = "
+person2.age); System.out.println("Reference of person2 object = "
+person2); } }
Output:
Reference of this keyword = Person@15db9742 Detail using this keyword, name = Rahul age = 20 Reference of person object = Person@15db9742 Detail using object, name = Rahul age = 20 Reference of this = Person@6d06d69c Detail using this keyword, name = Pradeep age = 22 Detail using object2, name = Pradeep age = 22 Reference of person2 object = Person@6d06d69c
Notice the reference of object and this
keyword, both are same which means this
keyword refers the same object which invokes the method. Using
this
keyword you can refer the properties
of that object, as you can see we are able to access name
and age
property of the object which invoked the method.
Let's understand it by the code given above. When code line Person person =
get's executed, the constructor get's called, so inside the constructor new
Person("Rahul"
, 20)this
keyword will refer the person
object as it invoked
the constructor.
Similarly when code line person.printDetail()
calls
the printDetial
method, inside the method this
keyword will point to person
object as it's the one
which called the method.
Is it possible to assign reference to this ?
No, doing(this
= objName) so will result in compilation error.
Use of this keyword in method and constructor
One of the very common use of this
keyword inside method or constructor is to access the properties of the object which invoked it. We can also invoke a method
or constructor using this
keyword in java.
Another good use of this
keyword is to resolve instance variable and parameter name conflict/ambiguity. If an instance variable name and parameter name is same,
this
keyword can be used to refer the instance variable inside the method or constructor to avoid the
name conflict/ambiguity.
Java program of this keyword uses inside method and constructor
class
Employee {String
name, address;int
age;public
Employee(String name,int
age) {this
(name, age,"NA"
);// calling constructor
}public
Employee(String name,int
age,String
address) {// this.name refers name instance variable while using only //name refers name parameter, this way we resolve name conflict.
this
.name = name;this
.age = age;this
.address = address; }void
printDetail() { System.out.println("Name = "
+this
.name+", age = "
+this
.age); System.out.println("Address = "
+this
.address);this
.checkVoterEligibility(this
.age);// calling method using this keyword
}void
checkVoterEligibility(int
age) {if
(age > 18) { System.out.println("You are eligible for vote"
); }else
{ System.out.println("You are not eligible for vote"
); } }public static void
main(String [] args) { Employee employee =new
Employee("Rahul"
, 20); employee.printDetail(); Employee employee2 =new
Employee("Pradeep"
, 22,"xyz"
); employee2.printDetail(); } }
Output:
Name = Rahul, age = 20 Address = NA You are eligible for vote Name = Pradeep, age = 22 Address = xyz You are eligible for vote
Can we call method using this keyword inside constructor ?
Yes, we can call the method from inside constructor, just use this
.methodName().
Can we use this keyword to access static method or variable ?
Yes we can, but accessing static members using this
keyword is not a good programming style.
Note : In java, when we call an instance method or variable without any object, java compiler internally adds this
keyword for invoking that method or
variable as shown in the program below :
class
Test { String name;void
m1() { n1(); System.out.println("Name = "
+name); }void
n1() { }public static void
main(String [] args) { Test test =new
Test(); test.m1(); } }// While compiling above class, the compiler will add this keyword inside m1 method like below
void
m1() {this
.n1(); System.out.println("Name = "
+this
.name); }
Different Uses of this keyword in Java
- The most common use of
this
keyword is to access the instance properties/variable of object which invokes the method or constructor. - If there is ambiguity between instance variables and parameters name,
this
keyword can be used to resolve the ambiguity inside the method/constructor. - We can use
this
keyword to call constructor inside another constructor. If calling a constructor inside another constructor, then constructor call must be the first statement inside the calling constructor. - It can be used to call a method from inside a method or constructor.
- It can be passed as an argument inside a method call.
- It can be used to return current object from a method.
How this keyword is different from normal reference variable
- We can not assign any reference(object) to
this
explicitly while a normal reference variable can refer to any other reference variable of same type. - A normal reference variable can be created directly inside a class but
this
keyword can not be used outside of method, constructor or block. - We can not use
this
keyword inside static block or method but we can create normal reference variable inside static block/method.