Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of "this" in Java?

Tags:

java

this

People also ask

What is this () in Java?

The this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.

Can we use this () in a method?

The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Yes, you can call methods using it.


this refers to the current object.

Each non-static method runs in the context of an object. So if you have a class like this:

public class MyThisTest {
  private int a;

  public MyThisTest() {
    this(42); // calls the other constructor
  }

  public MyThisTest(int a) {
    this.a = a; // assigns the value of the parameter a to the field of the same name
  }

  public void frobnicate() {
    int a = 1;

    System.out.println(a); // refers to the local variable a
    System.out.println(this.a); // refers to the field a
    System.out.println(this); // refers to this entire object
  }

  public String toString() {
    return "MyThisTest a=" + a; // refers to the field a
  }
}

Then calling frobnicate() on new MyThisTest() will print

1
42
MyThisTest a=42

So effectively you use it for multiple things:

  • clarify that you are talking about a field, when there's also something else with the same name as a field
  • refer to the current object as a whole
  • invoke other constructors of the current class in your constructor

The following is a copy & paste from here, but explains very well all different uses of the this keyword:

Definition: Java’s this keyword is used to refer the current instance of the method on which it is used.

Following are the ways to use this:

  1. To specifically denote that the instance variable is used instead of static or local variable. That is,

    private String javaFAQ;
    void methodName(String javaFAQ) {
        this.javaFAQ = javaFAQ;
    }
    

    Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absence of the this denotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective of this is used or not it denotes the instance variable.

  2. this is used to refer the constructors

     public JavaQuestions(String javapapers) {
         this(javapapers, true);
     }
    

    This invokes the constructor of the same java class which has two parameters.

  3. this is used to pass the current java instance as parameter

    obj.itIsMe(this);
    
  4. Similar to the above this can also be used to return the current instance

    CurrentClassName startMethod() {
         return this;
    }
    

    Note: This may lead to undesired results while used in inner classes in the above two points. Since this will refer to the inner class and not the outer instance.

  5. this can be used to get the handle of the current class

    Class className = this.getClass(); // this methodology is preferable in java
    

Though this can be done by

    Class className = ABC.class; // here ABC refers to the class name and you need to know that!

As always, this is associated with its instance and this will not work in static methods.


To be complete, this can also be used to refer to the outer object

class Outer {
    class Inner {
        void foo() {
            Outer o = Outer.this;
    }
  }
}

It refers to the current instance of a particular object, so you could write something like

public Object getMe() {
    return this;
}

A common use-case of this is to prevent shadowing. Take the following example:

public class Person {
    private final String name;

    public Person(String name) {
        // how would we initialize the field using parameter?
        // we can't do: name = name;
    }
}

In the above example, we want to assign the field member using the parameter's value. Since they share the same name, we need a way to distinguish between the field and the parameter. this allows us to access members of this instance, including the field.

public class Person {
    private final String name;

    public Person(String name) {
        this.name = name;
    }
}