I am currently learning java. I am a beginner and I don't understand the difference between a private variable in a class with getter and setter and a public variable. In fact, we can give a value from another class to the variable and recover the value also from another class in both cases.
Asked
Active
Viewed 38 times
0
-
The getter and setter allows to control it better, you could only write the getter, the setter can verify the given value to check if it's value or not, and you cannot do this with public attributes where there is no control – azro Mar 29 '20 at 09:59
-
5Does this answer your question? [Why use getters and setters/accessors?](https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors) – Eldar B. Mar 29 '20 at 10:06