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

后端 未结 5 1080
眼角桃花
眼角桃花 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

    Like ktm mentioned, setters tend to use the same name as the field for the parameter. In this case, the parameter shadows the field name, so

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

    would just set the parameter to itself rather than setting the field to the parameter.

提交回复
热议问题