+ 1
Acces modifiers
What's the purpose of using access modifiers for variables like private ot protected? In my understanding if i used public everywhere there wouldn't be complication.
2 Réponses
+ 2
Always make variables private and accessed them through getters and setters, you can later change them from a variable to a computed value, and then even later add caching to the computation for performance reasons. When it would be a public variable, you would have to change code everywhere the variable is used. But when you expose it to the outside world through getters and setters, all other code can keep using the class as if nothing had changed.
+ 1
https://en.m.wikipedia.org/wiki/Encapsulation_(computer_programming)