java this keyword

前端 未结 7 1510
野的像风
野的像风 2020-12-11 09:00

I have read that in Java you don\'t have to explicitly bind the this keyword to object, it is done by interpreter. It is opposite to Javascript where you

7条回答
  •  Happy的楠姐
    2020-12-11 09:46

    From the official documentation (found here):

    Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

    What this means is that inside the code of any class, when you write this, you specify the fact that you are referring to the current object.

    As a side note, you cannot use this with static fields or methods because they do not belong to any specific object (instance of the class).

提交回复
热议问题