Java Conventions: use getters/setters WITHIN the class?

前端 未结 12 1819
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 02:32

My professor really emphasizes protecting against privacy leaks by always using accessors and mutators to access private instance variables; however, do I have to use the ge

12条回答
  •  青春惊慌失措
    2020-11-30 02:56

    No you can use directly your instance variables inside the class, you're not violating any "rule". Getters and setters are mandatory for others classes to access instance variables of a class to not violate the encapsulation principle (which is quite important in OO programming).

    In the end it's a matter of choice, but you're saving one method call using your first example.

提交回复
热议问题