Learn This Keyword In Java (In Simple Language)
In this tutorial, you will learn the keyword this in Java; You will learn how and where to use it with the help of an example.
This keyword
In Java, this refers to the current object in methods or constructs. Let’s give an example to prove it.
- class MyClass {
- int instVar;
- MyClass (int instVar) {
- this.instVar = instVar;
- System.out.println (“this reference =” + this);
- }
- public static void main (String [] args) {
- MyClass obj = new MyClass (8);
- System.out.println (“object reference =” + obj);
- }
- }
Output
this reference = com.ThisAndThat.MyClass@74a14482
object reference = com.ThisAndThat.MyClass@74a14482
Note that the obj and this object identifiers are the same. This means nothing more than referring to the current object.
There are 3 situations in which this keyword is commonly used.
1. Use this to separate different variable sources
In Java you are not allowed to define two or more variables with the same name in a domain (class domain or method domain). However, the sample variables and parameters may have the same name. As:
class MyClass {
int instVar; // instVar instance variable
MyClass (int instVar) {// instVar parameter
instVar = instVar;
}
}
In the above program, the Java compiler is mistaken due to the ambiguity of the name. Therefore, to solve this problem, the keyword this is used.
Let’s look at an example without using this keyword:
- class MyClass {
- int instVar;
- MyClass (int instVar) {
- instVar = instVar;
- }
- public static void main (String [] args) {
- MyClass mc = new MyClass (8);
- System.out.println (“mc.instVar =” + mc.instVar);
- }
- }
Output
mc.instVar = 0
You might expect the output to be 8, but instead printed 0 because the Java compiler makes a mistake due to the name ambiguity between the sample variable and the constructor parameter.
Now, let’s rewrite the above code and use this keyword to solve this problem.
- class MyClass {
- int instVar;
- MyClass (int instVar) {
- this.instVar = instVar;
- }
- public static void main (String [] args) {
- MyClass obj = new MyClass (8);
- System.out.println (“obj.instVar =” + obj.instVar);
- }
- }
Output
mc.instVar = 8
You will now get the expected output. Because when you create an object, the Java compiler knows which object the constructor used.
When the Java compiler calls the constructor, this is replaced inside the constructor by the object that called the constructor.
Note: If you submit a parameter that has a different name than the sample variables, the compiler will automatically attach the this keyword.
This code
class MyClass {
int instVar;
MyClass (int i) {
instVar = i;
}
}
Is equal to:
class MyClass {
int instVar;
MyClass (int i) {
this.instVar = i;
}
}
Another common use of this keyword is in the get and set methods of a class. For example:
- class POJO {
- String name;
- void setName (String name) {
- this.name = name;
- }
- String getName () {
- return this.name;
- }
- public static void main (String [] args) {
- POJO pojo = new POJO ();
- pojo.setName (“Toshiba”);
- System.out.println (“pojo.name:” + pojo.getName ());
- }
- }
Output
pojo.name: Toshiba
2- Using this in overloading manufacturers
When working with builders overloading, it may be more useful to call one builder than another. However, the creators can not be explicitly called. Therefore, you can use another form of this keyword to achieve this goal. (() this)
The pseudo-code is shown here:
this (arg-list)
Here’s how to use this in the manufacturer’s call.
- class Complex {
- private int a, b;
- // parameterize constructor
- private Complex (int i, int j) {
- this.a = i;
- this.b = j;
- }
- private Complex (int i) {
- this (i, i); // invokes Complex (int i, int j);
- }
- private Complex () {
- this (0); // invokes Complex (int i);
- }
- @Override
- public String toString () {
- return this.a + ”+” + this.b + “i”;
- }
- public static void main (String [] args) {
- Complex c1 = new Complex (2, 3);
- Complex c2 = new Complex (3);
- Complex c3 = new Complex ();
- System.out.println (c1);
- System.out.println (c2);
- System.out.println (c3);
- }
- }
Output
2 + 3i
3 + 3i
0 + 0i
In the above program, it does not matter which constructor is called when introducing the object, finally the parametric constructor is called.
Be careful when using this (). Constructors calling this () run slowly because calling an additional constructor adds another overhead. If your class is only used to create a handful of objects, using this () is useful. Another great advantage of using this () is the reduction of duplicate code.
3- Send this as an argument
If you need to move the current object as an argument to a method, you can use this.
- class ThisExample {
- int x;
- int y;
- ThisExample (int x, int y) {
- this.x = x;
- this.y = y;
- System.out.println (“Before passing this to addTwo () method:”);
- System.out.println (“x =” + this.x + “, y =” + this.y);
- addTwo (this);
- System.out.println (“After passing this to addTwo () method:”);
- System.out.println (“x =” + this.x + “, y =” + this.y);
- }
- void addTwo (ThisExample o) {
- ox + = 2;
- oy + = 2;
- }
- }
- class Demo {
- public static void main (String [] args) {
- ThisExample obj = new ThisExample (1, -2);
- }
- }
Output
Before passing this to addTwo () method:
x = 1, y = -2
After passing this to addTwo () method:
x = 3, y = 0
Here, the addTwo () method is called with this as an argument from within the constructor.