I got a question about getters and setters, I'm wondering for some time now, about the best approach of setting/getting private or protected fields in the class' code to which those fields belong. (in the constructor for example)
I used to go about this by just using the fields directly like this:
But lately I've been using the getters and setters inside of the class' code like this:
In the given example this won't make any difference. But I thought it might be better to use the getters and setters in case that there'll be some restrictions in setters or some modifications done in getters, so these modifications or restrictions (or whatever will be going on in the setters and getters ;)) will be centralized.
I would like to hear the opinion of you guys about this, how do you set or get values of variables inside of the class that you are currently developing, does my second approach make any sense? Is it good or bad practice?
Thanks in advance for your input!