'this' in Java: How does it work?

前端 未结 7 453
孤街浪徒
孤街浪徒 2020-12-11 16:50

I know that \'this\' is acting as reference in Java. We can use it inside the class members only.

What I am asking is... because it is used in the members of the cl

相关标签:
7条回答
  • 2020-12-11 17:14

    "this" is implicitly passed as argument for every non-static method call you make. You can think of it as syntactical sugar, but at machine level, it is indeed an additional parameter which gets passed.

    class Person
    {
      string name;
      int age;
      void print(){ System.out.writeln(name+" "+age); }
    }
    

    works like that (pseudo-code):

    class Person
    {
      string name;
      int age;
    }
    
    void print(Person this)
    {
      System.out.writeln(this.name+" "+this.age);
    }
    
    0 讨论(0)
  • 2020-12-11 17:15

    this will give you a reference to the actual instance. It's very common in constructors, eg:

    class Dog {
       String name;
    
       public Dog(String name) {
          this.name = name;
       }
    }
    
    0 讨论(0)
  • 2020-12-11 17:22

    How exactly was [this] defined in Java?

    In Java this is defined by the Java Language Specification as a keyword and therefore is not an ordinary variable name and has special behaviour.

    The behaviour of this is defined in section 15.8.3 of the specification. For example:

    When used as a primary expression, the keyword this denotes a value, that is a reference to the object for which the instance method was invoked (§15.12), or to the object being constructed.

    0 讨论(0)
  • 2020-12-11 17:24

    Instance methods are invoked within the context of an actual instance, an actual object.

    Within the instance method of a class, this (in most context) refers to this object. This is a relative reference: when the same method is invoked on another object, then this within the execution of that method now refers to this second object.

    For completeness, it should be mentioned that Java also has what is called "qualified" this that can be used to refer to not the object that the method is invoked upon, but the enclosing object of an inner class.

    It can also appear, with some restriction, in constructors, to invoke another constructor of the same class. super can also be used in this manner.

    References

    • JLS 15.8.3 this
      • When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method was invoked, or to the object being constructed.
    • JLS 15.8.4 Qualified this
      • Any lexically enclosing instance can be referred to by explicitly qualifying the keyword this.
    • JLS 8.8.7.1 Explicit Constructor Invocations
      • Alternate constructor invocations begin with the keyword this (possibly prefaced with explicit type arguments). They are used to invoke an alternate constructor of the same class.
    0 讨论(0)
  • 2020-12-11 17:26

    You can think of 'this' refer to itself. It's like me.[attribute] to access itself attributes or methods.

    0 讨论(0)
  • 2020-12-11 17:31

    One way of thinking about it is as an implicit parameter to instance methods and constructors. In the case of a constructor it's the job of the constructor to set up "this", while in the case of instance methods, they can operate on "this" as appropriate.

    Note that Java also has some syntactic sugar allowing you to implicitly reference fields and methods of "this" without specifying this.foo.

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