Using “this” with class name

前端 未结 7 1483
暗喜
暗喜 2020-11-30 18:19

I am doing Android programming and was learning about Intents, when I saw a constructor that, to my C# trained mind, seemed funky. The call was:

Intent myI         


        
相关标签:
7条回答
  • 2020-11-30 18:45

    It's confusing only because when you use "MainActivity.this", it seems that you are referring to the class and not the object. In reality when you use "this" you are always referring to the current object, as the java se documentation states:

    https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

    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.

    It's just syntactically peculiar.

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