In this tutorial, you will learn the keyword this in Java. An example will help you learn how and where to use it.
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 three situations in which this keyword is commonly used.
1. Use this to separate different variable sources
In Java, you cannot 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; } }
The Java compiler is mistaken in the above program due to the ambiguous names. 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. Still, instead, it printed zero because the Java compiler made a mistake due to the name ambiguity between the sample variable and the constructor parameter.
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. When you create an object, the Java compiler knows which object the constructor used.
When the Java compiler calls the constructor, the object that called the constructor replaces it inside the constructor.
Note: If you submit a parameter with a different name than the sample variables, the compiler will automatically attach this keyword.
This code
class MyClass { int instVar; MyClass (int i) { instVar = i; } }
It is equal to:
class MyClass { int instVar; MyClass (int i) { this.instVar = i; } }
Another everyday use of this keyword is in a class’s get and set methods. 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, calling one builder over another may be more useful. 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 reducing duplicate code.
3- Send this as an argument
You can use this if you need to move the current object as an argument to a method.
- 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.