It is always recommended to use getter/setter for accessing private variables. Why would it not be a better idea to declare them as public and access them. Anyway we are accessing it using getter and setter?
It is always recommended to use getter/setter for accessing private variables. Why would it
Share
@mre answer is excellent and your question is fundamental. To summarize : you put the fields of an object private to gain control over the way it will be used by other objects. Your object uses setter to:
it will use getter to
welcome in the wonderfull world of OO programming and the magics of structured programming.
Stéphane