+ 1
getter and setter
can anyone help me with understanding what is getter and setter in java, in a proper way.
1 Antwort
+ 3
Accessing a class's data outside of the class is consider a bad practice. Therefore, the class provides a getter and setter to let outsiders access it. This allows the class the freedom to do all kinds of hidden stuff with its storage.
For example, say you create a class that provides the date. You could store the month, day, and year as individual integers or you could store it as days since computers existed. You could even not store anything, but call into the operating system to change or retrive it's date. The getter and setter would always be the same, but what the class stores isn't.