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 Build a Simple Android App Improving Our Code Simple Refactoring: Creating a New Class

Derick Brown
Derick Brown
7,361 Points

Why is the class FactBook public?

I'm still a little new to the "private vs public" concept when it comes to Java but I'm still learning lol. So with that being said, why would the class FactBook be public? I would think to set this to private so it can't be changed by anyone (assuming a user can somehow access this information from the front-end).

If my question doesn't make any sense then maybe I need a little more clarity on when to set something to private vs public so feel free to give it to me! lol

2 Answers

Public means other classes can access. Private means only that class can access. In between there is protected. Protected means other classes can see it, but other packages can not.

For now, while you learn, I would just default to public unless a tutorial or example says otherwise.

FactBook is a public class because all classes are public. More specifically, you want other classes (like the main class) to access it, or its methods. This means it will need to be public.

Derick Brown
Derick Brown
7,361 Points

Ahhh okay cool! Thanks for the explanation Mr.Philip.

No problem. Do you mind up-voting my answer though?

Derick Brown
Derick Brown
7,361 Points

Yes sir. Sorry about that.