Java setting private fields inside constructors

前端 未结 10 664
独厮守ぢ
独厮守ぢ 2021-01-17 15:48

Common design practice is to make instance variables private and have public getters and setters to access them. But many times I have seen code samples on the internet that

10条回答
  •  情话喂你
    2021-01-17 16:12

    Setting variables to private is to encourage encapsulation from other classes.

    Unless setName(String) was meant to do something extra (which the method name doesn't imply), it's unnecessary to use the setter while you're in the class where the private variable is.

提交回复
热议问题