When should I use “this” in a class?

后端 未结 18 2160
情书的邮戳
情书的邮戳 2020-11-21 23:36

I know that this refers to a current object. But I do not know when I really need to use it. For example, will be there any difference if I use x i

18条回答
  •  一向
    一向 (楼主)
    2020-11-22 00:06

    Following are the ways to use ‘this’ keyword in java :

    1. Using this keyword to refer current class instance variables
    2. Using this() to invoke current class constructor
    3. Using this keyword to return the current class instance
    4. Using this keyword as method parameter

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

提交回复
热议问题