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 Google Play Services Interacting with Your API Writing Your Adapter Class

Binyamin Friedman
Binyamin Friedman
14,615 Points

Is it ok to use public fields in a class?

It seems like in general it is a bad idea to have public fields, but in this course we have been using them in our model objects. Is there a good time to use public vs. private fields?

Mathew Tran
Mathew Tran
Courses Plus Student 10,205 Points

Public should be used when you want anyone to use your methods. Private should be used by default to abstract implementation away or 'unneeded information' from your users.

There are quite a few reasons for this, but here are a few:

  • Having all methods and properties public allow users to use your class in the way you didn't intend --> This may cause users to encounter errors or break your class
  • Having everything public clutters intellisense or autocomplete :)

An example I can think of is a simple light switch. I am no electrician -- but hopefully this will make sense. The interface that is public to you, is the ability to toggle it on or off. The underlying implementation of opening and closing of circuits to power a light would be private.

If you have everything public for the light switch, it would wouldn't be very safe! Imagine a light switch with open wires sticking out!

When you create a class, it usually serves a purpose or a problem, but how it is solved isn't exactly needed.

Hope this helps!

Matt.

2 Answers

Boban Talevski
Boban Talevski
24,793 Points

I might not be of much help as I haven't worked on a big commercial project so far, but from my understanding getters and setters should be the preferred way. And as you say, all the teachers before used this way. And from what I've seen around the web, it is the more appropriate way.

I believe that the teacher in this course didn't bother with these "non important" things as the project is fairly small (same reason he didn't bother with ButterKnife I guess), but I assume in a larger project, you would want all model objects with private fields and appropriate getters and setters. You might even have some additional "getters" which will get the property in a bit of a different way, like we used to get the day of the week in the Stormy app when we had a private date field, but we didn't exactly wanted the date, we just wanted the day of the week and we created a different "getter" method to get that (if I remember correctly, but that's the point anyway). Not sure if this would be considered a getter method though, as it isn't simply returning the value of the field.

Anyway, the reason above is probably one of the reasons why it's best to keep all these fields private and expose only the getters and setters, it will make the code cleaner and you wouldn't bother with the details behind the implementation once you complete it.

And code completion would really bother me if I'll be getting the fields every time I use an object of the said class. To me, they would just be interfering with my coding if I'm able to see them. I just have the impression it's not "natural" to have them public and exposed. Something like the exposed wires of the light switch in the comment above.

EDIT: I just realized this usage of public fields is probably because of retrofit as it maps the exact field names from the JSON response to the field names of the model object class. This is also probably why the fields Shop and Images in the Listing class start with capital letters, which isn't a common Java practice, but the fields in the JSON response have these exact names. I guess it'll become clearer as I progress through the course.

Binyamin Friedman
Binyamin Friedman
14,615 Points

I meant to say, "When should I use getters and setters vs. having public fields?"

In all other courses, the teachers have used getters and setters, but here they were using public fields.