Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,793 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.