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 Creating the MVP Remaining Tries

Confused about the use of the public constant MAX_MISSES

In the video it is mentioned that the constant MAX_MISSES should definitely be declared as public. Why is that? I'm a bit confused by that since other classes aren't accessing the constant directly and we are only using the constant in the getRemainingTries method of the class itself.

Shouldn't it be private in that case or should it be public by convention?

James Gray
James Gray
8,568 Points

I would assume that it's public so that other classes would be able to access it and know the max number of misses allowed in the future. With it being static and final it also cannot be changed anywhere besides in the game.java file.

2 Answers

Dan Bearden
Dan Bearden
24,231 Points

He said its public because as a user, you want to know how many guesses you can have. That being said, I would also argue that it doesn't HAVE to be public. You could make it private in the class, and create a public function called getMaxGuesses that returns the private variable.

Personally, if I were to develop this application on my own, I would actually make it private and use a public function to return the value to whatever needs it instead of accessing it directly from the class - its just better practice.

sandro mirijanashvili
sandro mirijanashvili
2,686 Points

It's public because when play the game you should know how many misses can you make.