getter and setter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

getter and setter

can anyone help me with understanding what is getter and setter in java, in a proper way.

26th Oct 2017, 12:17 PM
Kartik Soni
Kartik Soni - avatar
1 Answer
+ 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.
26th Oct 2017, 12:35 PM
John Wells
John Wells - avatar