Java this Keyword
In Java, this is a keyword that refers to the current object, the object whose method or constructor is being executed. It is mainly used to:
- Refer to the current class’s instance variables and methods.
- Differentiate between instance variables and local variables when they have the same name.
- Pass the current object as an argument to methods or constructors.
- Call one constructor from another within the same class (using this()).
The this keyword can be used to access instance variables and methods of the object on which the method or constructor is being invoked.
Below is the implementation of "this" reference:
public class Person {
// Fields Declared
String name;
int age;
// Constructor
Person(String name, int age)
{
this.name = name;
this.age = age;
}
// Getter for name
public String get_name() { return name; }
// Setter for name
public void change_name(String name)
{
this.name = name;
}
// Method to Print the Details of the person
public void printDetails()
{
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
System.out.println();
}
// main function
public static void main(String[] args)
{
// Objects Declared
Person first = new Person("ABC", 18);
Person second = new Person("XYZ", 22);
first.printDetails();
second.printDetails();
first.change_name("PQR");
System.out.println("Name has been changed to: "
+ first.get_name());
}
}
Output
Name: ABC Age: 18 Name: XYZ Age: 22 Name has been changed to: PQR
Explanation:
- The constructor uses this to refer to the current object’s name and age fields.
- The printDetails() method uses this to access instance variables of the current object.
- Each object (first and second) has its own copy of instance variables, and this helps differentiate between them.
Methods to Use "this" in Java
Following are the ways to use the "this" keyword in Java mentioned below:
1. Using "this" to Refer to Current Class Instance Variables
class Geeks {
int a;
int b;
// Parameterized constructor
Geeks(int a, int b)
{
this.a = a;
this.b = b;
}
void display()
{
// Displaying value of variables a and b
System.out.println("a = " + a + " b = " + b);
}
public static void main(String[] args)
{
Geeks object = new Geeks(10, 20);
object.display();
}
}
Output
a = 10 b = 20
2. Using this() to Invoke Current Class Constructor
class Geeks {
int a;
int b;
// Default constructor
Geeks()
{
this(10, 20);
System.out.println(
"Inside default constructor \n");
}
// Parameterized constructor
Geeks(int a, int b)
{
this.a = a;
this.b = b;
System.out.println(
"Inside parameterized constructor");
}
public static void main(String[] args)
{
Geeks object = new Geeks();
}
}
Output
Inside parameterized constructor Inside default constructor
3. Using "this" Keyword to Return the Current Class Instance
class Geeks {
int a;
int b;
// Default constructor
Geeks()
{
a = 10;
b = 20;
}
// Method that returns current class instance
Geeks get() {
return this;
}
// Displaying value of variables a and b
void display()
{
System.out.println("a = " + a + " b = " + b);
}
public static void main(String[] args)
{
Geeks object = new Geeks();
object.get().display();
}
}
Output
a = 10 b = 20
4. Using "this" Keyword as a Method Parameter
class Geeks {
int a;
int b;
// Default constructor
Geeks()
{
a = 10;
b = 20;
}
// Method that receives "this"
// keyword as parameter
void display(Geeks obj)
{
System.out.println("a = " + obj.a
+ " b = " + obj.b);
}
// Method that returns current class instance
void get() {
display(this);
}
// main function
public static void main(String[] args)
{
Geeks object = new Geeks();
object.get();
}
}
Output
a = 10 b = 20
5. Using "this" Keyword to Invoke the Current Class Method
class Geeks {
void display()
{
// calling function show()
this.show();
System.out.println("Inside display function");
}
void show()
{
System.out.println("Inside show function");
}
public static void main(String args[])
{
Geeks g1 = new Geeks();
g1.display();
}
}
Output
Inside show function Inside display function
6. Using "this" Keyword as an Argument in the Constructor Call
class A {
B obj;
// Parameterized constructor with object of B
// as a parameter
A(B obj)
{
this.obj = obj;
// calling display method of class B
obj.display();
}
}
class B {
int x = 5;
// Default Constructor that create an object of A
// with passing this as an argument in the
// constructor
B() { A obj = new A(this); }
// method to show value of x
void display()
{
System.out.println("Value of x in Class B : " + x);
}
public static void main(String[] args)
{
B obj = new B();
}
}
Output
Value of x in Class B : 5
Advantages of Using "this" Reference
There are many advantages of using "this" reference in Java as mentioned below:
- It helps to distinguish between instance variables and local variables with the same name.
- It can be used to pass the current object as an argument to another method.
- It can be used to return the current object from a method.
- It can be used to invoke a constructor from another overloaded constructor in the same class.
Disadvantages of Using "this" Reference
Although "this" reference comes with many advantages there are disadvantages of also:
- Overuse of this can make the code harder to read and understand.
- Using this unnecessarily can add unnecessary overhead to the program.
- Using this in a static context results in a compile-time error.
- Overall, this keyword is a useful tool for working with objects in Java, but it should be used judiciously and only when necessary.