Table of Contents
this keyword in java is used to refer to current object or instance of class. It can be used in the constructor to call any other overloaded constructor but this keyword should be the first statement in the constructor.
This keyword can be used for instance variables
this keyword can be used to refer to the instance variable of class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package org.arpit.java2blog; public class Employee { String name; int age; public Employee(String name,int age) { this.name=name; this.age=age; } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee("John",20); System.out.println("Employee's name : "+e1.getName()); System.out.println("Employee's age : "+e1.getAge()); } } |
When you run above program, you will get below output:
Employee’s age : 20
As you can see we have used this keyword to set values for instance variable in the constructor.
This keyword can be used to call the overloaded constructor
If you want to call overloaded constructor of same class, you can use this keyword to do that.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package org.arpit.java2blog; public class Employee { String name; int age; public Employee() { System.out.println("Calling No arg constructor"); } public Employee(String name,int age) { this(); System.out.println("Calling Parameterized constructor"); this.name=name; this.age=age; } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee("John",20); System.out.println("Employee's name : "+e1.getName()); System.out.println("Employee's age : "+e1.getAge()); } } |
When you run above program, you will get below output:
Calling Parameterized constructor
Employee’s name : John
Employee’s age : 20
Please note that this keyword used for calling another constructor should be first statement in that constructor.
this keyword can be used return object of the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package org.arpit.java2blog; public class Employee { String name; int age; public Employee(String name,int age) { this.name=name; this.age=age; } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Employee getEmployee() { return this; } public static void main(String args[]) { Employee e1=new Employee("John",20); Employee e1Copy=e1.getEmployee(); System.out.println("Employee's name : "+e1Copy.getName()); System.out.println("Employee's age : "+e1Copy.getAge()); } } |
Employee’s name : John
Employee’s age : 20
that’s all about this keyword in java.