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

carlorigacci
carlorigacci
4,777 Points

What was the point of setting the parameter of the Prompter's constructor to (Game game)?

Craig created the constructor of the Prompter class thusly:

public Prompter (Game game) { this.game = game; }

Don't pay attention to what is inside of the body of the constructor, what I care about and can't explain is why he had to add the (Game game). Later on in the video he creates a Prompter object inside of Hangman.java and it looks like this:

Prompter prompter = new Prompter(game);

And above that line he has this:

Game game = new Game("treehouse");

The way these two lines are interacting with each other ends up being that the Prompter object is essentially telling us that its parameter is "treehouse", correct? So it would essentially look like: Prompter("treehouse");

Did I understand this correctly? And if I did, then what was the point of this? I don't understand why he would do this.

1 Answer

Gabriel Precup
Gabriel Precup
4,565 Points

Not really, no. The object game that is now used in the Prompter has the variable answer equal to “treehouse”, the game object isn’t a String (it’s of Game type) and can’t be equal to a String. As a conclusion, the game you’re playing is based on the word “treehouse”.