+ 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.

26th Jul 2018, 7:43 PM
Adam
Adam - avatar
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.
26th Jul 2018, 8:11 PM
Sneher Ador
Sneher Ador - avatar
26th Jul 2018, 8:11 PM
KrOW
KrOW - avatar