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! While you're at it, check out some resources Treehouse students have shared here.

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

Android

Bastian Petersson
Bastian Petersson
9,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?

2 Answers

Tonnie Fanadez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

Bastian Petersson

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 Tavakoli
Fatemah Tavakoli
13,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.