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

Java Java Objects (Retired) Harnessing the Power of Objects Methods and Constants

Public/Private Java

This isn't a question as much as it is a cry for understanding. I just don't get public vs private. I understand the words being said, but in practice and theory I don't get it. Private keeps 'people' from changing the value or a certain variable in an object. But where and how are these 'people' changing the variables anyway? How does it prevent the changing of variables?

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

People in this case are other developers. Other developers are going to use your classes, because they are going to be amazing, and they are going to want to use it.

Anything you want those other developers to use, you make publicand anything you want to keep to yourself you make private. This way, they only know about the methods you want them to know about, and you can keep everything else to yourself...the big benefit is you can change any code that hasn't yet been exposed (and already used).

I am in the process of re-writing this course at the moment, and I really value feedback like this. I have worked this in to the new script specifically, so thank you for bringing it up!

Does that clarify things? Happy to discuss more!

It most definitely does. Thing really cleared up for me when I realized that it was mainly about the development of code and not 'security'. If that makes sense. I was like 'Who in the world is changing code?' But then as I said, it's other developers using reusing my code. So thank you both for the answers.

Simon Coates
Simon Coates
28,694 Points

Glad you got it. There are a lot of ways in which private methods or variables result in better quality code and increased ease of development. It's not just about other coders using your code. You'll often consume your own code, possibly years after you've forgotten about its inner workings.

Simon Coates
Simon Coates
28,694 Points

it means that code that has access to the class can't access the private stuff. If code can't access the parts of another class that it shouldn't need to access, then that class is more predictable, has a smaller public interface, and reduces coupling (a class shouldn't know anything about the interior workings of another class beyond what it absolutely HAS TO. The more a class relies on the inner workings of another class, the more exposed it is to changes in that class). Private variables and methods are more a protection against accidentally writing bad code, aid with maintainability and mean that a class can be treated simply (you don't have to worry about what's going inside, you just need to know how to use it). Hope that helps.