Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Bastian Petersson9,164 Points
Should we always make the getter and setter functions?
Should we always make the getter and setter functions, also if they are never used? and why?
Tonnie FanadezUX Design Techdegree Graduate 22,796 Points
You should always create getters and setters. By using getter and setter, the programmer can control how their important variables are accessed and updated.
Fatemah Tavakoli13,797 Points
Getter and setter are part of Encapsulation meaning we want to make sure that our sensitive data are not open to public. To successfully hide the data from the user, we create a class with private variables. Then we provide the public getter and setter methods to access and update the value of these private variables. However, if the value of the variable is not intended to be changed, you do not need to have a setter. You can only initialize it with the constructer one time and then use the getter to get it.