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

Can someone, in depth, explain this very small amount of code for me?

public class Prompter{ private Game mGame; public Prompter(Game game) { mGame = game;


I don't understand why are passing the object Game game to the prompter constructor, I also don't understand why we are storing it as a member variable or why we are making it private. Please break it down as understandable as possible please and thank you! :)

Eric Stermer
Eric Stermer
9,499 Points

As far as I see it, there are properties of the Game class that you want to carry over and be used in the Prompter class. Because your code is limited to just that one line it is hard to say.

Essentially, you have a class called Game where you can create objects of games such as Hangman or Scrabble or Tic Tac Toe. These games have specific properties such as Titles, rules to the game, a game board, and so on.

Next you have a Prompter which is the display in which a user can then interact with the game. So for the prompter to know what to out put and get input from the user, it needs to know the game it is playing. This is maybe why you would pass the Object of game into the Prompter.

You then have a private variable called mGame created so that it is local to just the Prompter class and not recognized in other classes. Meaning if you go back to the Game class and call upon the values in mGame you cannot use it. To give mGame a value, you set it in the constructor of Prompter by setting mGame to the value of what you passed to the constructor, which in this case is game.

Now in the Prompter class, everytime you need the values that the game can provide, you can just use mGame.