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 Prompting for Guesses

Imran Asif
Imran Asif
7,514 Points

why declaring Object of class Game as member variable instead of creating new instance with new keyword

why declaring Object of class Game as member variable instead of creating new instance with new keyword Game game= new Game(); and then we can use game variable to get applyGuess () method

what is the difference between these two approaches ?

1 Answer

This is the first part I interpret your question like : "Why " The reason he made it private is because of the encapsulation. Meaning Craig makes member variables private. The other reason is that it is not needed to make a new Game object. Everything that happens, needs to happen in the same Hangman match and not a new one .

What i also take from your question is : "Why is the private Game game added as a member variable in Prompter ?" The other reason is as follows : the Prompter handles the logic for the input for the Game and the display of it ( if you're not that far, it's comming up pretty soon ). The prompter like i said handles the input from the user. When the User gives input, it needs to check what it is. Whether its right or wrong falls outside of the scope of the prompter class. So now that game is a member variable, it can acces it and use the applyGuess function to determine wether it was a hit or a miss.

hoped this helped a bit Imran Asif . If ya need more explanation just tag me.