In Java, ‘this’ is a reference variable that refers to the current object, or can be said “this” in Java is a keyword that refers to the current object instance. It can be used to call current class methods and fields, to pass an instance of the current class as a parameter, and to differentiate between the local and instance variables. Using “this” reference can improve code readability and reduce naming conflicts.
Java this reference Example
In Java, this is a reference variable that refers to the current object on which the method or constructor is being invoked. It can be used to access instance variables and methods of the current object.
Below is the implementation of this reference:
Java
public class Person {
String name;
int age;
Person(String name, int age)
{
this .name = name;
this .age = age;
}
public String get_name() { return name; }
public void change_name(String name)
{
this .name = name;
}
public void printDetails()
{
System.out.println( "Name: " + name);
System.out.println( "Age: " + age);
System.out.println();
}
public static void main(String[] args)
{
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
In the above code, we have defined a Person class with two private fields name and age. We have defined the Person class constructor to initialize these fields using this keyword. We have also defined getter and setter methods for these fields which use this keyword to refer to the current object instance.
In the printDetails() method, we have used this keyword to refer to the current object instance and print its name, age, and object reference.
In the Main class, we have created two Person objects and called the printDetails() method on each object. The output shows the name, age, and object reference of each object instance.
Methods to use ‘this’ in Java
Following are the ways to use the ‘this’ keyword in Java mentioned below:
- Using the ‘this’ keyword to refer to current class instance variables.
- Using this() to invoke the current class constructor
- Using ‘this’ keyword to return the current class instance
- Using ‘this’ keyword as the method parameter
- Using ‘this’ keyword to invoke the current class method
- Using ‘this’ keyword as an argument in the constructor call
1. Using ‘this’ keyword to refer to current class instance variables
Java
class Test {
int a;
int b;
Test( int a, int b)
{
this .a = a;
this .b = b;
}
void display()
{
System.out.println( "a = " + a + " b = " + b);
}
public static void main(String[] args)
{
Test object = new Test( 10 , 20 );
object.display();
}
}
|
2. Using this() to invoke current class constructor
Java
class Test {
int a;
int b;
Test()
{
this ( 10 , 20 );
System.out.println(
"Inside default constructor \n" );
}
Test( int a, int b)
{
this .a = a;
this .b = b;
System.out.println(
"Inside parameterized constructor" );
}
public static void main(String[] args)
{
Test object = new Test();
}
}
|
Output
Inside parameterized constructor
Inside default constructor
3. Using ‘this’ keyword to return the current class instance
Java
class Test {
int a;
int b;
Test()
{
a = 10 ;
b = 20 ;
}
Test get() { return this ; }
void display()
{
System.out.println( "a = " + a + " b = " + b);
}
public static void main(String[] args)
{
Test object = new Test();
object.get().display();
}
}
|
4. Using ‘this’ keyword as a method parameter
Java
class Test {
int a;
int b;
Test()
{
a = 10 ;
b = 20 ;
}
void display(Test obj)
{
System.out.println( "a = " + obj.a
+ " b = " + obj.b);
}
void get() { display( this ); }
public static void main(String[] args)
{
Test object = new Test();
object.get();
}
}
|
5. Using ‘this’ keyword to invoke the current class method
Java
class Test {
void display()
{
this .show();
System.out.println( "Inside display function" );
}
void show()
{
System.out.println( "Inside show function" );
}
public static void main(String args[])
{
Test t1 = new Test();
t1.display();
}
}
|
Output
Inside show function
Inside display function
6. Using ‘this’ keyword as an argument in the constructor call
Java
class A {
B obj;
A(B obj)
{
this .obj = obj;
obj.display();
}
}
class B {
int x = 5 ;
B() { A obj = new A( this ); }
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 certain 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 certain 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
21 Sep, 2023
Like Article
Save Article