Java - when “this” is the only way to go?

后端 未结 5 1068
眼角桃花
眼角桃花 2021-01-28 13:21

The following code runs for both var = putVar; & this.var = putVar;

I understand: \"this\" is used to identify that - \"put this value for just \'my\'

相关标签:
5条回答
  • There are 2 cases I know of, aside from the case ktm mentioned (which I think is obvious and you already knew):

    1. Just to make it very clear that they're referring to the member of the current object.

      void foo(int x) {
          this.y = x; // No mistaking that y belongs to the object
      }
      
    2. If you're in an anonymous inner class within another object (ex: of class ClassName), you can use ClassName.this to get the instance of the enclosing object. The reason for this (no pun intended) is that, inside the inner class, this will refer to the inner class.

      SomeInnerClass myObj = new SomeInnerClass() {
          void bar() {
              this.y = 0; // this refers to the SomeInnerClass object
              OuterClass.this.y = 0; // OuterClass.this refers to the enclosing class OuterClass object
          }
      };
      
    0 讨论(0)
  • 2021-01-28 13:25

    If a class has an instance variable with some name (say myVar), and a method has a LOCAL variable with the exact same name (myVar), then any reference to the variable will refer to the LOCAL variable. In cases such as these, if we want to specify the class's instance variable, we need to say this.myVar. As stated before, this is particularly useful when we want to have setters in which the parameter name is the same as the instance variable that it sets:

    public void setMyVar(int myVar) {
        this.myVar = myVar; // If we said myVar = myVar;, we'd just set the local
                            // variable to itself, which would NOT be what we want.
    }
    
    0 讨论(0)
  • 2021-01-28 13:35

    You can use this if you're using the same method argument identifier as a field, but it can be avoided if you simply do not use the same name.

    Not using the same name is a more common practice to avoid confusion and shadowing. Hence, any reference to this in a setter can be replaced with a better naming standard: inParameter, for instance.

    public void setX(int inX) {
        x = inX;
    }
    

    The other use of this would be to explicitly invoke a constructor. This is a form which can't be replaced with a simpler naming convention:

    public class Foo {
    
        private String name;
    
        public Foo() {
            this("");
        }
    
        public Foo(String inName) {
            name = inName;
        }
    }
    

    There may also be a case in which you want to return the instance you're working with. This is also something that this allows you to do:

    return this;
    
    0 讨论(0)
  • 2021-01-28 13:37

    Because people like to use the same variable name for both the method parameter and the instance variable - in which case you need this to differentiate.

    public void setX(int x) {
        this.x = x;
    }
    
    0 讨论(0)
  • 2021-01-28 13:37

    Like ktm mentioned, setters tend to use the same name as the field for the parameter. In this case, the parameter shadows the field name, so

    public void setX(int x) {
        x = x;
    }
    

    would just set the parameter to itself rather than setting the field to the parameter.

    0 讨论(0)
提交回复
热议问题