Java - when “this” is the only way to go?

后端 未结 5 1114
眼角桃花
眼角桃花 2021-01-28 13:21

The following code runs for both var = putVar; & this.var = putVar;

I understand: \"this\" is used to identify that - \"put this value for just \'my\'

5条回答
  •  悲&欢浪女
    2021-01-28 13:37

    Because people like to use the same variable name for both the method parameter and the instance variable - in which case you need this to differentiate.

    public void setX(int x) {
        this.x = x;
    }
    

提交回复
热议问题