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 trialKevin Gresmer
3,020 PointsAlternate way to print out the correct answer?
public void play() { while (mGame.getRemainingTries() > 0 && !mGame.isSolved()) { displayProgress(); promptForGuess(); } if (mGame.isSolved()) { System.out.printf("Congratulations you won with %d tries remaining!", mGame.getRemainingTries()); } else { System.out.printf("Bummer the word was %s. :(\n", mGame.getAnswer()); } }
In the code above we had to make a method, getAnswer, to establish a variable for the answer.
Why can't we just say;
System.out.printf("Bummer the word was %s. :(\n", game.mAnswer);
mAnswer is a string with the answer in it. Why doesnt that work?
2 Answers
Craig Dennis
Treehouse TeachermAnswer
is private
Fernando Szymczak
17,372 PointsI feel like I'm missing the point on the reason for making mAnswer private. I'm pretty sure there must be a good reason for this, but I just keep wondering why would I want to go the trouble of making the code lengthier and in a way that requires adding extra functions, when a public mAnswer would allow me to do mGame.mAnswer to get the answer. The logic behind this must have just gone straight over my head. I wonder if I'm missing the actual meaning of private in Java, as in private from who or what?? And is it necessary when it comes to hangman or is it just the practice in Java development?
Craig Dennis
Treehouse TeacherHi Fernando!
Keeping your fields private allow you to expose how your object is intended to work. If we only make one getAnswer method, that means that you cannot change the answer, only retrieve it. You are right, it is a Java practice and the idea is relatable to the concept of properties in other languages. The answer is that it is private to any code outside of the current class.
You are also right that it is extra work, good news is most IDEs (and Code Editors) do all the monotonous stuff for you. We'll get there shortly!
That help?
Timothy McCune
6,744 PointsIn this example, it doesnt make a lot of sense to make the property private. However, this is an entry level course on programming. Therefore, it is a good idea to teach us good programming practices. And keeping class properties off limits to other classes or other parts of the application is a good programming practice. Imagine this were a gigantic application, with hundreds of classes and multiple people working on the project. You start to get odd behavior with the propert. With a public property, u might have to sift through a million lines of code, to figure out who is changing the propertiies value. Wasting valuable time debugging, when u could have made the property private from the beginning and using getter and setter functions to change or retrieve its value.
Hope that made sense, Tim
Timothy McCune
6,744 PointsTimothy McCune
6,744 PointsThe Prompter class is not allowed to access our Game classes private member variables. In fact, the only class that can access the Game classes private member variables, is the game class.