'this' in Java: How does it work?

前端 未结 7 455
孤街浪徒
孤街浪徒 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: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;
       }
    }
    

提交回复
热议问题